调用其他模型的回调方法



我正在使用CakePHP 2.9。我想在父母的afterSave中称孩子afterSave

这是我的模型及其回调方法:

父模型

/*
* @property Child @Child
*/
class Parent extends AppModel {
public $hasMany = [
'Child' => array(
'className' => 'Child',
'foreignKey' => 'parent_id',
'dependent' => false,
)
];
public function afterSave($created, $options = array()){
if( ! $created && $this->data['Parent']['status'] == 0 ) {
// Update child's status
}
}
}

儿童模型

/*
* @property Grandchild @Grandchild
*/
class Child extends AppModel {
// belongs to Parent
public $hasMany = [
'Grandchild' => array(
'className' => 'Grandchild',
'foreignKey' => 'child_id',
'dependent' => false,
)
];
public function afterSave($created, $options = array()){
if( ! $created && $this->data['Child']['status'] == 0 ) {
// Update grandchild's status
}
}
}

我怎样才能在家长afterSave中称呼孩子的afterSave

你能不能不做这样的事情:

class Parent extends AppModel {
// ..
public function afterSave($created, $options = array()){
if(!$created && $this->data['Parent']['status'] == 0) {
$child = new Child();
$child->afterSave($created, $options);
}
}
}

最新更新