R
R
ragnar_ok2019-09-05 20:50:41
1C-Bitrix
ragnar_ok, 2019-09-05 20:50:41

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'),
        ];
    }
}

Now I call:
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`))"
}

Naturally , createDbTable() only creates one table. How to create a developer_region for communication?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
ragnar_ok, 2019-09-07
@ragnar_ok

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`)
);

Or describe the entity of the linking table and use createDbTable()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question