CakePHP 3.1: Сохранить в joinTable тот же внешний ключ, но разные _joinData

У меня есть эта ассоциация в модели, которую я хочу сохранить:

$this->belongsToMany('Tags', [
'className' => 'Tags',
'joinTable' => 'tags_associations',
'foreignKey' => 'foreign_key',
'targetForeignKey' => 'tag_id'
]);

Я хочу сохранить в связанной таблице tags_associations 2 записи, которые имеют одинаковые Tags.id но разные _joinData значение. Это пример

object(Companies\Model\Entity\Company) {

'id' => (int) 765,
'tags' => [
(int) 0 => object(Cake\ORM\Entity) {

'id' => (int) 2,
'tag_category_id' => (int) 1,
'level' => (int) 1,
'parent_id' => (int) 1,
'lft' => (int) 2,
'rght' => (int) 135,
'tag_name' => 'Abbigliamento',
'slug' => 'abbigliamento',
'description' => null,
'created' => object(Cake\I18n\Time) {

'time' => '2015-10-04T17:56:02+0200',
'timezone' => 'Europe/Rome',
'fixedNowTime' => false

},
'modified' => object(Cake\I18n\Time) {

'time' => '2015-10-05T10:03:53+0200',
'timezone' => 'Europe/Rome',
'fixedNowTime' => false

},
'_joinData' => object(Cake\ORM\Entity) {

'model' => 'Companies',
'tag_group_id' => (int) 2,
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'model' => true,
'tag_group_id' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'TagsAssociations'

},
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'_joinData' => true
],
'[original]' => [
'_joinData' => object(Cake\ORM\Entity) {

'model' => 'Companies',
'tag_group_id' => (int) 1,
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'model' => true,
'tag_group_id' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'TagsAssociations'

}
],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Tags'

},
(int) 1 => object(Cake\ORM\Entity) {

'id' => (int) 2,
'tag_category_id' => (int) 1,
'level' => (int) 1,
'parent_id' => (int) 1,
'lft' => (int) 2,
'rght' => (int) 135,
'tag_name' => 'Abbigliamento',
'slug' => 'abbigliamento',
'description' => null,
'created' => object(Cake\I18n\Time) {

'time' => '2015-10-04T17:56:02+0200',
'timezone' => 'Europe/Rome',
'fixedNowTime' => false

},
'modified' => object(Cake\I18n\Time) {

'time' => '2015-10-05T10:03:53+0200',
'timezone' => 'Europe/Rome',
'fixedNowTime' => false

},
'_joinData' => object(Cake\ORM\Entity) {

'model' => 'Companies',
'tag_group_id' => (int) 2,
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'model' => true,
'tag_group_id' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'TagsAssociations'

},
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'_joinData' => true
],
'[original]' => [
'_joinData' => object(Cake\ORM\Entity) {

'model' => 'Companies',
'tag_group_id' => (int) 1,
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'model' => true,
'tag_group_id' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'TagsAssociations'

}
],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Tags'

}
],
}

В моем примере только вторая сущность хранится в таблице tags_association.

0

Решение

Вы используете использовать through вариант из http://book.cakephp.org/3.0/en/orm/associations.html#using-the-through-option

class StudentsTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Courses', [
'through' => 'CourseMemberships',
]);
}
}

class CoursesTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Students', [
'through' => 'CourseMemberships',
]);
}
}

class CoursesMembershipsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Students');
$this->belongsTo('Courses');
}
}

Теперь CourseMemberships Модель — это отдельная модель, и вы можете создать столько, сколько захотите.

0

Другие решения

Наконец, я решил с другой ассоциацией

$this->belongsToMany('Tags', [
'className' => 'Tags',
'joinTable' => 'tags_associations',
'foreignKey' => 'foreign_key',
'targetForeignKey' => 'tag_id',
]);

$this->belongsToMany('TagsSecondary', [
'className' => 'Tags',
'joinTable' => 'tags_associations',
'foreignKey' => 'foreign_key',
'targetForeignKey' => 'tag_id',
'saveStrategy' => 'append'
]);

Сначала я сохраняю теги tag_group_id = 1 с тегами ассоциаций и после тех с tag_group_id = 2 с TagsSecondary, который имеет присоединять как saveStrategy.

0

Вы можете загрузить модель в свой контроллер

$this->loadModel('tagAssociations');

и после этого вы можете создать новую сущность из этой таблицы, исправить ее и, наконец, сохранить.

//saving tags
$tagAssociation = $this->tagAssociations->newEntity();
//$handling $data
$tagAssociation = $this->tagAssociations->patchEntity($tagAssociation , $data);
$this->tagAssociations->save($tagAssociation );

Это лучшее решение, которое я нашел.

0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector