Exemple #1
0
 /**
  * Toggle field value.
  *
  * @param Table|\Cake\ORM\Table $table
  * @param string|int $id
  * @param string|int $value
  * @param string $field
  * @throw BadRequestException
  * @throw RuntimeException
  */
 public function fieldToggle($table, $id, $value, $field = self::TOGGLE_DEFAULT_FIELD)
 {
     $this->_checkIsAjax();
     $this->_checkToggleData($id, $value);
     $this->_controller->viewBuilder()->layout('ajax')->templatePath('Common');
     $entity = $table->get($id);
     $entity->{$field} = !(int) $value;
     if ($result = $table->save($entity)) {
         $this->_controller->set('record', $result);
         $this->_controller->render('toggle');
     } else {
         throw new RuntimeException(__d('union', 'Failed toggling field {0} to {1}', $field, $entity->{$field}));
     }
 }