0

I have this association in model that i want to save:

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

I want to save in associated table tags_associations 2 records which have same Tags.id but different _joinData value. This is an example

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'

        }
    ],
}

In my example only second entity is kept in tags_association table.

Oops D'oh
  • 941
  • 1
  • 15
  • 34
DarioLap
  • 177
  • 2
  • 13

3 Answers3

0

You use use the through option from 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');
    }
}

Now the CourseMemberships model is a individual model, and you can create as many as you want.

Spriz
  • 457
  • 2
  • 9
  • In my case don't work: I haven't _company_id_ as foreign_key in tags_assocations, but _model_ and _foreign_key_ – DarioLap Oct 05 '15 at 10:54
  • I see.. You could take a look at some of the Behaviours around, I guess eg. the translation behaviours has samme issue. – Spriz Oct 05 '15 at 14:25
0

Finally, I have resolved with another association

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

First i save Tags with tag_group_id = 1 with Tags Associations and after those with tag_group_id = 2 with TagsSecondary, that has append as saveStrategy.

DarioLap
  • 177
  • 2
  • 13
0

You can load the model in your controller

$this->loadModel('tagAssociations');

and after that you can create new entity from this table, patch it and finally save it.

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

It's the best solution i have found.

Matoran
  • 114
  • 1
  • 6