簡體   English   中英

兩個模型使用相同的表但在CakePHP中具有條件

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

我有兩個要保存在同一表中的模型。 例如,我有一個狀態模型和一個工資表模型,都應保存在狀態表中。 但是在檢索狀態模型時,應僅返回付款=“否”的記錄,而付款時間表僅返回付款=“是”的記錄。 在每個模型中,我都會有一個保存前的代碼,以確保將正確的付款值保存到表中。 我的問題是,如何在不對每次find()操作執行此操作的情況下,將對模型表的檢索限制為上述約束?

ps我還沒有弄清楚,我是CakePHP新手。

應該可以在模型的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM