CakePHP belongsToMany jointable -
i'm making blog , want able assign multiple categories or tags article , article can have multiple categories.
this have in database: articles, categories , join table articles_categories.
project:
join table:
in 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' ]); }
in 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' ]); }
when user adds article, needs add article , ids in join table here's articlescontroller/add method:
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 add 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();
and after submitting form 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.' ]
my problem i'm not sure if associations correct. how automatically insert data in join table?
the property name
that not correct format belongstomany
association, see
- http://book.cakephp.org/3.0/en/orm/associations.html#belongstomany-associations
- http://book.cakephp.org/3.0/en/views/helpers/form.html#associated-form-inputs
the property name should default underscored, plural name of association, in case categories
.
the magic _ids
key
since want link article existing categories, should additionally use magic _ids
key pass ids of selected categories, marshaller take care of rest, pick , insert appropriate category
entities based on given ids, see
http://book.cakephp.org/3.0/en/orm/saving-data.html#converting-belongstomany-data
all need append field/propertyname, categories._ids
, ie form input should defined this:
$this->form->input('categories._ids', [ 'options' => $categories, 'id' => 'magicselect', 'multiple' => true, 'class' => 'form-control' ]);
note it's not necessary explicitly pass options, if follow conventions, ie use underscored plural of association name, form helper able automagically pick variable other input fields.