2010-08-14 62 views
1

我有兩個模型,我想保存在同一個表中。 例如我有一個狀態模型和一個payschedule模型都應該保存在狀態表中。但是,在檢索狀態模型時,應只返回付款='否'的記錄,而付款日程只記錄付款='是'。 我將在每個模型中保存之前確保將正確的支付值保存到表中。 我的問題是,我怎樣才能限制從模型表上的檢索到上面解釋的約束,而不必在每個find()操作中執行?兩個模型使用同一個表,但在CakePHP條件

ps我你還沒弄明白,我是一個CakePHP noob。

回答

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

+0

謝謝!將嘗試並反饋。 – RaScoop 2010-08-14 16:29:21

+0

謝謝!奇蹟般有效! ......回想起來,我只能想到一件事......杜赫! – RaScoop 2010-08-14 16:57:13

相關問題