0

保存したいモデルにこの関連付けがあります:

$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'

        }
    ],
}

私の例では、2 番目のエンティティのみが tags_association テーブルに保持されます。

4

3 に答える 3

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 に答える
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 に答える