CakePHP属于ToMany jointable

时间:2015-04-09 23:20:42

标签: cakephp cakephp-3.0

我正在创建博客,我希望能够为文章分配多个类别或标签,而文章可以有多个类别。

这就是我在数据库中的内容:文章类别和联接表 articles_categories

PROJECT

enter image description here

加入表

enter image description here

在我的表/ ArticlesTable.php中:

 public function initialize(array $config)
 {
     $this->addBehavior('Timestamp');
     $this->belongsToMany('Categories', [
         'alias' => 'Categories',
         'foreignKey' => 'article_id',
         'targetForeignKey' => 'category_id',
         'joinTable' => 'articles_categories'
     ]);
 }

在我的表/ CategoriesTable.php中:

 public function initialize(array $config)
 {
     $this->table('categories');
     $this->displayField('name');
     $this->primaryKey('id');
     $this->addBehavior('Timestamp');

     $this->belongsToMany('Articles', [
         'alias' => 'Articles',
         'foreignKey' => 'category_id',
         'targetForeignKey' => 'article_id',
         'joinTable' => 'articles_categories'
     ]);
 }

当用户添加文章时,需要添加文章,然后添加联接表中的ID 这是我的ArticlesController / add方法:

 public function add()
 {
     $article = $this->Articles->newEntity();

     if ($this->request->is('post'))
     {
         $article = $this->Articles->patchEntity($article, $this->request->data);
         $article->user_id = $this->Auth->user('id');

         if ($result = $this->Articles->save($article))
         {
             $this->Flash->success(__('Your article has been saved.'));
             return $this->redirect(['action' => 'index']);
         }
         $this->Flash->error(__('Unable to add your article.'));
     }
     $this->set(['article'=>$article,'buttontext'=>'Add Article']);
     $categories = $this->Articles->Categories->find('list');
    $this->set(compact('categories'));
 }

add.ctp view

echo $this->Form->create($article, ['class' => 'form-group']); echo
$this->Form->input('articlecategory',['options' =>
$categories,'id'=>'magicselect','multiple'=>true,'class'=>'form-control']);
echo $this->Form->input('title',
['class'=>'form-control',
    'maxlength'=>'50']); echo $this->Form->input('body',
['rows' => '8',
    'class'=>'form-control',
    'style'=>'margin-bottom:10px;resize: none',
    'maxlength'=>'5000']); echo $this->Form->button(__($buttontext), ['class'=>'btn btn-success']);
echo $this->Form->end();

以及提交表单调试后得到的结果($ this-> request-> data):

[
    'articles_categories' => [
        (int) 0 => '1'
    ],
    'title' => 'test article',
    'body' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.'
]

我的问题是我不确定我的关联是否正确。 如何自动在连接表中插入数据?

1 个答案:

答案 0 :(得分:6)

属性名称

这不是belongsToMany关联的正确格式,请参阅

默认情况下,属性名称应为关联的下划线复数名称,因此在本例中为categories

魔法_ids

由于您要将文章与现有类别相关联,因此您还应该使用魔术_ids键来传递所选类别的ID,然后编组将处理其余的内容,即选择和根据给定的ID插入适当的Category实体,请参阅

<强> http://book.cakephp.org/3.0/en/orm/saving-data.html#converting-belongstomany-data

您需要做的就是将其附加到字段/属性名称,例如categories._ids,即您的表单输入应该像这样定义:

$this->Form->input('categories._ids', [
    'options' => $categories,
    'id' => 'magicselect',
    'multiple' => true,
    'class' => 'form-control'
]);

请注意,没有必要显式传递选项,如果您遵循约定,即使用下划线复数的关联名称,那么表单助手将能够自动获取变量就像它一样适用于其他输入字段。