Yii Framework Forum: Implicit Save Vs Explicit Save - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Implicit Save Vs Explicit Save Rate Topic: -----

#1 User is offline   awebdeveloper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 29-September 12

Posted 06 February 2013 - 01:26 PM

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.
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 06 February 2013 - 02:11 PM

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.

Quote

CExistValidator validates that the attribute value exists in a table.
This validator is often used to verify that a foreign key contains a value that can be found in the foreign table.


Regards.
0

#3 User is offline   awebdeveloper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 29-September 12

Posted 06 February 2013 - 03:36 PM

View Postseenivasan, on 06 February 2013 - 02:11 PM, said:

Dear Friend

I prefer second one.

We can use CExistValidator

Regards.


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

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 06 February 2013 - 05:15 PM

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.
0

#5 User is offline   awebdeveloper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 29-September 12

Posted 07 February 2013 - 10:12 PM

View PostMike, on 06 February 2013 - 05:15 PM, said:

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users