2017-08-28 31 views

回答

0

我還沒有和cakePHP具體工作,但如果它是類似於其他一些MVC框架中,beforeSave()afterSave()回調相似的上下文中執行(它們屬於同一個對象 - 一個模型或控制器)。

如果是這樣的話,一個簡單的解決方案是簡單地使用自定義類屬性來跟蹤您的數據。

private $custom; 

beforeSave(){ 
    // save what you want to access later 
    $this->custom = 'comparison result'; 
} 

afterSave(){ 
    // retrieve what we found in beforeSave() 
    $custom = $this->custom; 
} 
+0

真棒!謝謝! –

0

您可以將參數傳遞給你的回調從模型的save(array $data = null, array $params = array())功能beforeSave(array $options = array())afterSave(boolean $created, array $options = array())

下面是一個例子

//In your controller 
$this->Post->save($data, array('arg1' => $value)); 

//In the Post Model 
beforeSave($options = array()){ 
    // $options contains the arg1 param 
} 

afterSave($created, $options = array()){ 
    // $options contains the arg1 param 
} 

試試吧:)

相關問題