Edit Deselect All Doesn't Update

Hello again,

I’m facing with a new issue regarding with our update. Basically, We have a Keyword, Fbpages, Fbpagekeywords table. A Keyword can be assigned to multiple Fbpages, and a Fbpages can be assigned to multiple Keywords, and Fbpagekeywords is their join table. I generated the models using the cake bake model [tables] command, so it auto-generated the associations of the table in their models. Whenever I update a Keyword and deselect all the selected fbpages then save, it doesn’t save the new data which should be empty since I deselected them all, instead it still shows all the selected data. If I initially saved 5 selected, then edit and deselect 2 fbpages, it works properly. The next time I edit the keyword it shows 3 selected, but if I deselect all 3, it doesn’t work. Even a keyword with 1 selected fbpage before edit, and I deselect it it still shows the fbpage. It’s like preventing me to deselect all. It always shows at least 1 selected after edit.

Here’s the KeywordsTable.php:

public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('keywords');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('Fbpagekeywords', [
            'foreignKey' => 'keyword_id',
            'saveStrategy' => 'replace',
            'dependent' => true,
            'cascadeCallbacks' => true,
        ]);

        $this->hasMany('Fbpageposts', [
            'foreignKey' => 'keyword_id',
            'saveStrategy' => 'replace'
        ])
        ->setDependent(true);
    }

Here’s the FbpagesTable.php

public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('fbpages');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('Fbpageposts', [
            'foreignKey' => 'fbpage_id',
            'saveStrategy' => 'replace'
        ])
        ->setDependent(true);

        $this->hasMany('Fbpagekeywords', [
            'foreignKey' => 'fbpage_id',
            'saveStrategy' => 'replace'
        ])
        ->setDependent(true);
        

        $this->hasMany('Fbpagesharetimes', [
            'foreignKey' => 'fbpage_id',
            'saveStrategy' => 'replace',
        ])
        ->setDependent(true);
    }

And here’s the FbkeywordsTable.php

type or paste code herepublic function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('fbpagekeywords');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->belongsTo('Fbpages', [
            'foreignKey' => 'fbpage_id',
            'joinType' => 'INNER',
        ]);
        $this->belongsTo('Keywords', [
            'foreignKey' => 'keyword_id',
            'joinType' => 'INNER',
        ]);
    }

Here’s my KeywordsController.php - Edit

public function edit($id) {
    $this->loadModel('Fbpages');
    $this->loadModel('Fbpagekeywords');
    $fbpages = $this->Fbpages->find('all');
    $queues_items = $this->Fbpagekeywords->find('all');

    $keyword = $this->Keywords->findById($id)
                              ->contain(['Fbpagekeywords'])
                              ->firstOrFail();

    if ($this->request->is(['post', 'put'])) {

      $this->Keywords->patchEntity($keyword, $this->request->getData(), ['associated' => 'Fbpagekeywords']);
      if ($this->Keywords->save($keyword)) {
          $this->Flash->success(__('Keyword has been updated.'));
          return $this->redirect(['action' => 'index']);
      }
      $this->Flash->error(__('Unable to update your keyword.'));
    }

    $this->set(compact('keyword', 'fbpages'));
  }

To complete here’s my Keywords edit.php

<h1>Edit Keyword</h1>

<?php

  echo $this->Form->create($keyword);
  echo $this->Form->control('id', ['type' => 'hidden']);
  echo $this->Form->control('keyword');
  echo '<label class="article-fb-caption">Assign keyword to Facebook Pages:</label>';
  echo '<div class="article-fb-selection">';
  
  foreach($fbpages as $index => $fbpage) :
    $checked = false;
    
    foreach($keyword->fbpagekeywords as $keyword_item) {
      if($keyword_item['fbpage_id'] === $fbpage['id']) {
        $checked = true;
      } 
    }
  
  echo '<span class="article-fb-option">' 
    . $this->Form->checkbox( 'fbpagekeywords.'.$index.'.fbpage_id',
      ['value' => $fbpage['id'],
      'hiddenField' => false,
      'checked' => $checked
      ]) 
    . $fbpage['page_title'] . '</span>';
    endforeach;

  echo '</div>';
  echo $this->Form->button(__('Update'));
  echo $this->Form->end();

?>

I know I made a mistake somewhere, would be appreciated if someone could point me to the right direction. Thank you!

Just a guess, but have you tried it without 'hiddenField' => false?

Hello Zuluru, I’ve tried this before and I get the error “Unable to update your keyword.” which is the Flash->error() in edit.