Getting belongtomany data on edit page

I am using 3 tables:

newsmessages, groups and groups_newsmessages.

They have the following layout in the DB:

newsmessages:

  • id
  • title
  • content

groups:

  • id
  • group_name

groups_newsmessages:

  • id
  • group_id
  • newsmessage_id

So I am able to edit a newsmessage and add ids of groups I select (can be multiple) combined with the newsmessage id in the “groups_newsmessages” table, with the follow code:

public function edit($id = null)
{
    $this->set('title', 'Bewerk nieuwsbericht');

    $this->viewBuilder()->setLayout('admin');

    $newsmessage = $this->Newsmessages->get($id, [
        'contains' => ['Groups', 'Webpages']
    ]);

    if ($this->request->is(['patch', 'post', 'put'])) {
        $newsmessage = $this->Newsmessages->patchEntity($newsmessage, $this->request->getData());
        if ($this->Newsmessages->save($newsmessage)) {
            $this->Flash->success(__('The newsmessage has been saved.'));

            return $this->redirect(['action' => 'manage']);
        }
        $this->Flash->error(__('The newsmessage could not be saved. Please, try again.'));
    }
    $groups = $this->Newsmessages->Groups->find('list', [
        'keyField' => 'id',
        'valueField' => 'group_name']);
    $webpages = $this->Newsmessages->Webpages->find('all', [
        'conditions' => ['Webpages.isblog = ' => 1], 
        'limit' => 200
    ]);
    $this->set(compact('newsmessage', 'groups', 'webpages'));
}

On the edit page I have the code setup like:

<div class="form-group mt-3 show-groups d-none">
    <?= $this->Form->input('groups._ids', 
        [
            'options' => $groups,
            'id' => 'group_id',
            'class' => 'form-control'
        ]) 
    ?>
</div>

But what I am unable to do is pre-select groups I have added to the table already, so when loading the edit page from a specific newsmessage, it already shows the groups I have added the newsmessage to.

Thanks for any help.

When creating your form, are you passing in your $newsmessage variable?

$this->Form->create($newsmessage);

https://book.cakephp.org/3.0/en/views/helpers/form.html#starting-a-form

Also, depending on which version of CakePHP you’re using, input() has been deprecated. I suggest moving to control() instead – the syntax should be pretty similar.

input(): https://api.cakephp.org/3.8/class-Cake.View.Helper.FormHelper.html#_input
control(): https://book.cakephp.org/3.0/en/views/helpers/form.html#creating-form-controls

Thanks, I am using 3.8.3.

I changed it to use control, but when I add ‘multiple’ => true to it, it doesn’t show the checked items I have stored in the database.

btw, when I remove the multiple => true part, the select shows only 1 option that is stored in the DB, but not more than that.

I believe you need to use 'multiple' => 'checkbox' in order to produce a set of checkboxes. The documentation is a bit buried, but you can see it mentioned if you go to the following page:

https://book.cakephp.org/3.0/en/views/helpers/form.html#creating-select-pickers

And then search for Attributes for Select Pickers on the page. Per CakePHP, you’re technically creating a special version of a select picker.

You’ll end up with something like this:

<div class="form-group mt-3 show-groups d-none">
    <?= $this->Form->control('groups._ids', 
        [
            'multiple' => 'checkbox',
            'options' => $groups,
            'id' => 'group_id',
            'class' => 'form-control'
        ]) 
    ?>
</div>

I checked some of my own projects and this (in theory) works properly. I’ve used this many times myself, good luck!