如何更新一个表的字段,我的模型称为 User,我想更改 字段“启用”。 我就是这么做的。
function setstatus($id = null)
{
$this->User->id = $id;
if (!$this->User->exists())
{
$this->Session->setFlash('Invalid User', 'error');
$this->redirect(array('action' => 'index'));
}
$valor = false;
$enable = $this->User->read('enable');
if ($enable == false)
$valor = true;
$this->User->saveField('enable', $valor);
$this->redirect(array('action' => 'index'));
$this->Session->setFlash('User update success', 'info');
}
但这并不能正常工作。 不改变字段。请帮我... 我使用 Cakephp 2.3,如果不知道在 cakephp 2.3 中怎么做,也许就像 cakephp 1.3 一样。
最佳答案
我自己回答,解决方案是将 $this->ModelName->read(field);
的返回值作为 array ['model']['field' ]
代码如下:
function setstatus($id = null)
{
$this->User->id = $id;
if (!$this->User->exists())
{
$this->Session->setFlash('Invalid user', 'error');
$this->redirect(array('action' => 'administration'));
}
$enable= $this->User->read('enable');
$msj = 'The user has been enabled';
if ($enable['User']['enable'] == 1)
{
$enable= 0;
$msj = 'The user is no longer enable';
}
else
$enable= 1;
$this->User->saveField('enable', $enable);
$this->Session->setflash($msj, 'info');
$this->redirect(array('action' => 'administration'));
}
关于cakephp - 如何更新一个字段 cakephp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14984886/