2015-10-05 79 views
0

我有这种关联模型,我要保存: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表中。

回答

0

你使用现在CourseMemberships模型是一个单独的模型中使用through选项从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'); 
    } 
} 

,你可以创建你想要的。

+0

在我的情况下不工作:我没有_company_id_作为foreign_key在tags_assocations,但_model_和_foreign_key_ – DarioLap

+0

我看到..你可以看看周围的一些行为,我想例如。翻译行为有问题。 – Spriz

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与TagsSecondary,有追加saveStrategy解决。

0

您可以在控制器

$this->loadModel('tagAssociations'); 

加载模型之后,你可以从该表中创建新的实体,打补丁,最后保存。

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

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