简体   繁体   English

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

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

I have two models that I would like to save in the same table. 我有两个要保存在同一表中的模型。 For instance I have a status model and a payschedule model both should be saved in the statuses table. 例如,我有一个状态模型和一个工资表模型,都应保存在状态表中。 But at retrieving the status model should return only the records with payment = 'no' and the payschedule only records with payment = 'yes'. 但是在检索状态模型时,应仅返回付款=“否”的记录,而付款时间表仅返回付款=“是”的记录。 I will have a before save in each model to make sure that the correct payment value is saved to the table. 在每个模型中,我都会有一个保存前的代码,以确保将正确的付款值保存到表中。 My question is how can I restrict the retrieval from the table on the model to the constraints explained above without having to do it at each find() operation? 我的问题是,如何在不对每次find()操作执行此操作的情况下,将对模型表的检索限制为上述约束?

ps I you have not figured it out, I am a CakePHP noob. ps我还没有弄清楚,我是CakePHP新手。

It should be possible to implement this in the find() method of your model: 应该可以在模型的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);
}

edit: 编辑:

To follow the CakePHP recommendation, it should probably be implemented in the function beforeFind() instead: http://book.cakephp.org/view/1049/beforeFind 要遵循CakePHP的建议,它可能应该在beforeFind()函数中实现: http : //book.cakephp.org/view/1049/beforeFind

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

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