代码之家  ›  专栏  ›  技术社区  ›  Gaurav Sharma

仅从cakephp v1.3中的数据库获取“已启用”记录

  •  0
  • Gaurav Sharma  · 技术社区  · 16 年前

    是否有任何方法只获取cakephp v1.3中“status=1”模型的记录?

    我在Web应用程序的每个表中都创建了一个名为“status”的字段。

    I have a model named 'Message'. 我想要的是只显示那些消息,包括在“status=1”的搜索结果中。状态为“0”的邮件将不会显示,也不会包含在搜索结果中。

    Please help me if I can do it the cakePHP way, for now as a temporary solution I am using the 'status = 1' in the conditions array of every find of messages.

    谢谢

    3 回复  |  直到 16 年前
        1
  •  1
  •   Rob Wilkerson    16 年前

    我会把这个包括在 beforeFind() callback method. You can include it in the callback for every model (but only once for each model--not for each 查询 )或者你可以把它应用到你的 AppModel 所以你所有的其他模型都继承了它。

    在 预查找() 回调,可以修改查询的结构。在这种情况下,你可以应用这个条件 array( 'status' => 1 ) to your incoming query. Dump the argument data structure to see what's coming in and I think it will make sense to you.

    请注意,如果您选择在 AppMod 必须创建特定于模型的 beforeFind 以后回拨时,您需要确保您打过电话 parent::beforeFind() 在您的子类中' 预查找() 方法。

        2
  •  0
  •   Gaurav Sharma    16 年前

    找到答案了 here beforefind()回调非常方便。

    这样地

    public function beforeFind($conditions) {
        if(!isset($conditions['conditions']['Message.status']) && !isset($conditions['conditions']['status'])) {
            $conditions['conditions']['Message.status'] = 1;
        }
        return $conditions;
    }
    
        3
  •  0
  •   Nik Chankov    16 年前

    基本上,你说的是软可删除行为。有一个 here For me it's not perfect, but for simple app it could do the job. In the comments you can see the improvements too :)

    但是在AppModel中扩展beforefind()也应该可以。