繁体   English   中英

两个模型使用相同的表但在CakePHP中具有条件

[英]Two models using the same table but with conditions in CakePHP

我有两个要保存在同一表中的模型。 例如,我有一个状态模型和一个工资表模型,都应保存在状态表中。 但是在检索状态模型时,应仅返回付款=“否”的记录,而付款时间表仅返回付款=“是”的记录。 在每个模型中,我都会有一个保存前的代码,以确保将正确的付款值保存到表中。 我的问题是,如何在不对每次find()操作执行此操作的情况下,将对模型表的检索限制为上述约束?

ps我还没有弄清楚,我是CakePHP新手。

应该可以在模型的find()方法中实现这一点:

public function find($type, $options = array()) {

    // Make sure there is a 'conditions' array.
    if(!isset($options['conditions']))
        $options['conditions'] = array();

    // Overwrite conditions in $options with your automatic conditions.
    $options['conditions'] = array_merge(
        $options['conditions'],
        array('payment' => 'yes')
    );

    // Just pass them to the parent implementation.
    return parent::find($type, $options);
}

编辑:

要遵循CakePHP的建议,它可能应该在beforeFind()函数中实现: http : //book.cakephp.org/view/1049/beforeFind

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM