cakephp - 如何更新一个字段 cakephp

标签 cakephp set field

如何更新一个表的字段,我的模型称为 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/

相关文章:

css - 使用 CSS 在 Drupal 7 中排列字段显示

authentication - CakePHP - 两种类型的用户进行身份验证

php - 如何处理删除 JSON :API? 中的关系

CakePHP/CakePHP 2.4 应用程序

python - 如何从 Python 字典值中查找常见项?

java - 与在 Java 中表示集合的集合混淆?

c# - 使用父保护构造函数实例化子类字段

php - Cakephp3,如何在cakephp3中使用where子句和inner join?

php - 在 PHP 中管理 MySQL SET 数据类型

php - 从 PHP 和 HTML 中的回显文本字段获取输入?