0

我在要保存的模型中有这个关联:

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

我想保存在关联表tags_associations2 中具有相同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 表中。

4

3 回答 3

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二级的标签保存标签,这些标签附加saveStrategy

于 2015-10-05T12:39:59.727 回答
0

您可以在控制器中加载模型

$this->loadModel('tagAssociations');

之后,您可以从此表中创建新实体,对其进行修补并最终保存。

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

这是我找到的最好的解决方案。

于 2016-09-06T18:54:32.273 回答
0

您使用http://book.cakephp.org/3.0/en/orm/associations.html#using-the-through-optionthrough中的选项

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模型是一个单独的模型,您可以创建任意数量的模型。

于 2015-10-05T10:35:25.923 回答