我有这种关联模型,我要保存:CakePHP的3.1:保存joinTable相同的外键,但不同的_joinData
$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表中。
在我的情况下不工作:我没有_company_id_作为foreign_key在tags_assocations,但_model_和_foreign_key_ – DarioLap
我看到..你可以看看周围的一些行为,我想例如。翻译行为有问题。 – Spriz