This works: ($this->Task->save('active', '0')
This does not: ($this->Task->save('active', '1')
it fails model validation: Model->Task
'active' => array(
'boolean' => array(
'rule' => array('boolean'),
),
),
TaskController.php
This works:
public function deactivate ($id = null) {
$this->Task->id = $id;
if (!$this->Task->exists()) {
throw new NotFoundException(__('Invalid task'));
}
$this->request->onlyAllow('post');
if ($this->Task->save('active', '0')) {
$this->Session->setFlash(__('The task has been saved'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('The task could not be saved. Please, try again.'));
}
This does not:
public function activate ($id = null) {
$this->Task->id = $id;
if (!$this->Task->exists()) {
throw new NotFoundException(__('Invalid task'));
}
$this->request->onlyAllow('post');
if ($this->Task->save('active', 1)) {
$this->Session->setFlash(__('The task has been saved'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('The task could not be saved. Please, try again.'));
$this->redirect($this->referer());
}
}
Here is the call from View/Tasks/index.ctp:
<?php
if ($task['Task']['active'] == 1){
echo $this->Form->postLink(__('Deactivate'), array('action' => 'deactivate', $task['Task']['id']),null, __('Are you sure you want to return # %s to the project?', $task['Task']['id']));
} else {
echo $this->Form->postLink(__('Activate'), array('action' => 'activate', $task['Task']['id']),null, __('Are you sure you want to send # %s to your todo list?', $task['Task']['id']));
}
?>
mysql db: field 'active' is type "tinyint".
Also, the checkbox form control generated by Bake in Views/Tasks/edit.ctp works just fine.
I have also tried the following:
($this->Task->save('active', 1)
($this->Task->save('active', true)
($this->Task->save('active', 'true')
($this->Task->save('active', '2')
This:
($this->Task->save('active', `1`) //notice the tic marks
seems to bypass validation, but does not update the database.
This works: ($this->Task->save('active', '0')
Well, I doubt it :)
It might not give an error but it's not going to do what you expect. That's going to skip validating and try to process 'active'
as an array. The consequence of doing that is it's probably only bumping the modified
field time.
This does not: ($this->Task->save('active', '1')
That's dependent on your validation rules, but regardless that syntax is not going to do what you're expecting.
Please refer to the documentation, your parameters are wrong. You're passing the string "active" as your data, and your boolean-ish value as the value of $validate
.
This would work (or, not fail as being syntactically wrong):
$this->Task->save(array('active' => $value));
or this:
$this->Task->save(array('Task' => array('active' => $value)));
or by using saveField:
$this->Task->saveField('active', $value)
If in doubt - use bake, or compare to the code it generates.
Why dont you just follow the documentation? http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savefield-string-fieldname-string-fieldvalue-validate-false
There it clearly shows that you are misusing save().
You would need to use saveField() or updateAll() (atomic) for what you are doing.