CakePHP的:一列 - >一真 [英] CakePHP: One column -> one TRUE

查看:126
本文介绍了CakePHP的:一列 - >一真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人能够帮助我解决这个:

Hope someone could help me with this:

我想有将包含一个且只有一个 TRUE 值的列。我想 TRUE (或'1')的值设置为一行,并得到保证,其余的行是 FALSE (或'0')

I would like to have a column that will contain one and only one TRUE value. I want to set a value of TRUE (or '1') to a row and be assured that the remaining rows are FALSE (or '0').

如何可以使用CakePHP实现?

How this could be achieved with CakePHP?

推荐答案

您不能在数据库级执行本(当然,你可以,如果你使用触发器),所以你必须使用模型来执行呢:: beforeSave ()

You can't enforce this at the database level (well, you can if you use triggers) so you'll have to enforce it using Model::beforeSave()

线沿线的东西:

public function afterSave() {

    if ($this->data['Model']['field'] == 1) {
        // set all other records to false
        $this->updateAll(array('field'=>0));
    }

}

在保存记录,它应该是唯一一个田设置为true。

When the record is saved, it should be the only one with 'field' set to true.

这篇关于CakePHP的:一列 - >一真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆