簡體   English   中英

CakePHP 3.1:將相同的外鍵但不同的_joinData保存在joinTable中

[英]CakePHP 3.1: Save in joinTable same foreign key but different _joinData

我在要保存的模型中有此關聯:

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

我想將具有相同Tags.id但具有不同_joinData值的tags_associations 2記錄保存在關聯表中。 這是一個例子

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表中僅保留第二個實體。

您可以使用http://book.cakephp.org/3.0/en/orm/associations.html#using-the-through-option中的“ through選項

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模型是一個單獨的模型,您可以創建CourseMemberships模型。

最后,我與另一個協會解決了

$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保存具有tag_group_id = 1的標簽,並附加saveStrategy

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

$this->loadModel('tagAssociations');

然后,您可以從該表中創建新實體,對其進行修補並最終保存。

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

這是我發現的最佳解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM