开发者

Two models using the same table but with conditions in CakePHP

开发者 https://www.devze.com 2023-01-12 01:56 出处:网络
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 mode

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

精彩评论

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