1

I'm using CakePHP 2.9. I want to call Child's afterSave in Parent's afterSave.

Here are my models with their callback methods :

Parent Model

/*
 * @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
        }
    }
}

Child Model

/*
 * @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
        }
    }
}

How can I call Child's afterSave in Parent's afterSave?

Maya Nastasya
  • 197
  • 2
  • 16
  • create object of child class in parent `afterSave` method then call the child class `afterSave` method using created object – RAUSHAN KUMAR Jul 24 '17 at 06:02
  • @RAUSHANKUMAR Hello Raushan, it's not working. The callback method is not called. – Maya Nastasya Jul 24 '17 at 06:07
  • Where do you call this afterSave? First, you need to check method is called or not. Do print something on afterSave(); – 502_Geek Jul 25 '17 at 09:34
  • Hi @KevinKyaw , the afterSave method is called in each model's event (already checked). But if I do something to trigger the Parent's afterSave, the Child's afterSave is not triggered. – Maya Nastasya Jul 26 '17 at 02:41
  • Meaning, if you do something in Parent's aftersave, it's worked. But, child's aftersave is not worked right? How about the other way, if you do something on Child's aftersave, is Parent's aftersave is work ? – 502_Geek Jul 26 '17 at 03:55
  • @KevinKyaw Yes, Parent's afterSave also doesn't work if I do something on Child's afterSave. – Maya Nastasya Jul 26 '17 at 08:41
  • Can you provide some code that how do you call these ? Now, I can't see the whole things – 502_Geek Jul 26 '17 at 08:55
  • @KevinKyaw afterSave method is automatically called when a record of the model is saved – Maya Nastasya Jul 26 '17 at 09:57

1 Answers1

0

Can you not do something like this:

class Parent extends AppModel {
    // ..

    public function afterSave($created, $options = array()){
        if(!$created && $this->data['Parent']['status'] == 0) {
            $child = new Child();
            $child->afterSave($created, $options);
        }
    }
}
flygaio
  • 121
  • 5
  • Hi @flygaio , the `$this->data['Child']['status']` is only an empty array in Child's Model if I do as you suggested. How to send the data to Child's Model? The relationship is : Parent has many Child, Child has many Grandchild. – Maya Nastasya Jul 31 '17 at 02:57