用CakePHP读取对象属性
我第一次使用CakePHP,在我的控制器中,我需要切换模型实例属性的状态:用CakePHP读取对象属性,php,cakephp,cakephp-model,Php,Cakephp,Cakephp Model,我第一次使用CakePHP,在我的控制器中,我需要切换模型实例属性的状态: function toggleAutoMark($id = null) { $this->Test->id = $id; $this->Test->saveField('automark', !$this->Test->read('automark')); $this->redirect(array('controller' => 'tests',
function toggleAutoMark($id = null) {
$this->Test->id = $id;
$this->Test->saveField('automark', !$this->Test->read('automark'));
$this->redirect(array('controller' => 'tests', 'action' => 'view', $id));
}
我不知道我是否应该使用read()方法,但这将返回一个数组(这不是我认为文档中所说的)
不起作用,可能是因为实例尚未加载。您可能正在查找
Model::field()
因此
$this->Test->field('automark');
虽然从性能角度来看,这不是一个很好的主意
updateAll执行一个原子查询,这在这里更适合您(请参阅相关文档)
$this->Test->field('automark');