2

我有两个模型要保存在同一个表中。例如,我有一个状态模型和一个工资表模型都应该保存在状态表中。但是在检索状态模型时,应该只返回 payment = 'no' 的记录,而 payschedule 只返回 payment = 'yes' 的记录。我将在每个模型中进行之前保存,以确保将正确的付款值保存到表中。我的问题是如何将模型上的表的检索限制为上述约束,而不必在每个 find() 操作中进行?

ps 我还没有弄清楚,我是一个 CakePHP 菜鸟。

4

1 回答 1

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

于 2010-08-14T15:57:18.843 回答