开发者

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开发者_StackOverflow 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?

ps I you have not figured it out, I am a CakePHP noob.


It should be possible to implement this in the find() method of your model:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜