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

时间:2010-08-14 15:15:12

标签: php cakephp-1.3

我想将两个模型保存在同一个表中。 例如,我有一个状态模型和一个payschedule模型都应该保存在状态表中。但是在检索状态模型时,应仅返回pay ='no'的记录,而payschedule仅记录payment ='yes'的记录。 我将在每个模型中保存一个,以确保将正确的付款值保存到表中。 我的问题是如何将模型表中的检索限制为上面解释的约束,而不必在每次find()操作时都这样做?

ps我还没弄明白,我是CakePHP菜鸟。

1 个答案:

答案 0 :(得分:1)

应该可以在模型的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