Implicit Save Vs Explicit Save

Product - id, object_id

Media - id, object_id

Object - id, type(product,media)

Product table belongs to Object table

Media table belongs to Object table.

Whenever a Product/media is created an object_id needs to be created. So should i explicitly create a entry in object table and assign it to object_id in controller or do it in model in beforeSave method.

Also i don’t want product to be created if a object is not created. So if i am using 2nd way is there a way to prevent save of product if object is not created.

My preferred way is 2. Would like to know your thoughts on each one.

Dear Friend

I prefer second one.

We can make a check by two ways.

1.We can use CActiveRecord::exists method to check the existance of an object by ralational attribute,

in a custom validator method.

2.We can use CExistValidator

This is what YII says in API.


If i do it in beforesave i cannot use CExistValidator. Am i right here coz validation happens before this (beforesave) is executed

Create the Object record in the beforeSave() method in your Product/Media models. If the save() on the Object record returns false (=fails) then simply return false from beforeSave() and it will not save the Product/Media record.

Thanks so is there a way i can communicate back saying object creation failed