Answer the question
In order to leave comments, you need to log in
Bitrix D7 ORM: how to organize a many-to-many relationship (ManyToMany/N:M)?
Following the course , created the DeveloperTable entity . You need to associate this entity with a many-to-many relationship (ManyToMany) with the RegionTable entity (an entity of the same structure):
use Bitrix\Main\ORM\Data\DataManager;
use Bitrix\Main\ORM\Fields;
class DeveloperTable extends DataManager
{
public static function getTableName()
{
return 'developer';
}
public static function getMap()
{
return [
(new Fields\Relations\ManyToMany('REGION', RegionTable::class))->configureTableName('developer_region'),
new Fields\IntegerField('ID', [
'primary' => true,
'autocomplete' => true,
]),
new Fields\TextField('NAME'),
];
}
}
DeveloperTable::getEntity()->compileDbTableStructureDump()
Outputs:array(1) {
[0]=>
string(100) "CREATE TABLE `developer` (`ID` int NOT NULL AUTO_INCREMENT, `NAME` text NOT NULL, PRIMARY KEY(`ID`))"
}
Answer the question
In order to leave comments, you need to log in
You need to manually query the linking table. This is not provided by the framework today.
CREATE TABLE `developer_region` (
`developer_id` int(11) NOT NULL,
`region_id` int(11) NOT NULL,
PRIMARY KEY (`developer_id`,`region_id`),
CONSTRAINT `FK_developer` FOREIGN KEY (`region_id`) REFERENCES `region` (`id`),
CONSTRAINT `FK_region` FOREIGN KEY (`developer_id`) REFERENCES `developer` (`id`)
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question