activerecord update aftersave

I have some related tables in my DB, after save data in first I want to use new primary key to inserting in second table.to do that I use aftersave method in my model. but if I update first table after save method doesnt calls. Also class cactiverecord doesnt have afterupdate method. What is the better way to explain same functionality after update as aftersave?

thx

I don’t know the exact reason why there is no validation and no before/after-methods for update. But you can just use save. If you want to determine whether this is an INSERT or UPDATE in your before/after-methods you can use the isNewRecord property. If you want to prevent validation, use “save(false)”.

hth, yodel

can’t say how u r using but here is a sample code. The else part executes for update.


	protected function afterSave()

	{

		if($this->isNewRecord)

			echo ('created new.');

		else

			echo ('updating.');

		return true;

	}



You no need to use afterSave() in this case. I hope the following script helps to you


if($a->validate())

{

    if($b->validate())

    {

        $a->save(false);

        $b->ref_id = $a->id;//id is primary key and auto increment in $a

        $b->save(false);

    }

}

can be done either way, but if we r taking use input for the child table too then its better to first validate both then save. if parent is validated true and child has errors, u have to call update then or save child table separately.