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

时间:2015-10-05 10:08:05

标签: php cakephp cakephp-3.1

我想在模型中保存这个关联:

$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表中。

3 个答案:

答案 0 :(得分:0)

您可以使用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

现在String input = "Street1,Punjab Market-Patiala 147001 M:92166-29903"; int value = input.lastIndexOf(":"); String s = input.substring(value + 1, input.length()); Log.d("reverseString", "" + s); 模型是一个单独的模型,您可以根据需要创建任意数量的模型。

答案 1 :(得分: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保存标签,然后使用带有TagsSecondary的tag_group_id = 2保存标签,将追加保存为 saveStrategy

答案 2 :(得分:0)

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

$this->loadModel('tagAssociations');

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

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

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