Last Updated: February 25, 2016
·
1.215K
· zeroeth

CakePHP 3.0 HasAndBelongsToMany Input Broken on Edit

Upgrading CakePHP 3.0->3.1 will save mondo head scratching about check-boxs/multi-selects not working right for HasAndBelongsToMany.

3.0 will save the relations in the join table, but will not show them as selected on further edits.

All you should need in your view for it to work is:
(Example is Cart HasAndBelongsToMany Items), editing Cart.

$this->Form->input('Item', array('multiple' => 'checkbox'));

or

$this->Form->input('Item', array('multiple' => true));

And in your controller action

$this->set('items', $this->Cart->Item->find('list'));

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside