CakePHP belongsToMany jointable

CakePHP belongsToMany jointable

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

这是我数据库中的内容:文章类别和连接table articles_categories.

项目:

加入TABLE

在我的 Table/ArticlesTable.php:

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

在我的 Table/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'
     ]);
 }

用户添加文章时,需要先添加文章,然后在join中添加ids table 这是我的 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 查看:

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();

提交表单 debug($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.'
]

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

属性姓名

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

默认情况下,属性 名称应该是协会的带下划线的复数名称,因此在本例中 categories.

神奇的_ids钥匙

既然你想link你的文章到已经存在的类别,你应该另外使用神奇的_ids键来传递所选类别的ID,然后编组器会处理休息,即根据给定的 ID 选择并插入适当的 Category 实体,请参阅

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

您需要做的就是将其附加到 field/propertyname,如 categories._ids,即您的表单输入应定义如下:

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

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