Yii Framework Forum: Save and related record - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Save and related record Rate Topic: -----

#1 User is offline   omoratin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 23-November 09

Posted 23 November 2009 - 08:08 PM

Hi,

I was wondering if Yii saves related records. I've tried and it doesn't seem to work like Rails or Symfony.
Am I wrong?

If I am, could you provide an example?

Thanks a lot
0

#2 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 23 November 2009 - 08:37 PM

your means:

$post = Post::model()->findByPk(10);
$post->author->name = 'Jerry';
$post->save();

$post->author->name is changed to 'Jerry' ?


not supported seemingly~
0

#3 User is offline   omoratin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 23-November 09

Posted 24 November 2009 - 01:24 AM

No I meant something like

$post = new Post;
$post->author = new Author;

Then if I do $post->save(); the author gets also save and the foreign key is automatically assigned
0

#4 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 24 November 2009 - 02:30 AM

View Postomoratin, on 24 November 2009 - 01:24 AM, said:

No I meant something like

$post = new Post;
$post->author = new Author;

Then if I do $post->save(); the author gets also save and the foreign key is automatically assigned


This question let's qiang to answer, I want to know too~
0

#5 User is offline   Mike 

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

Posted 24 November 2009 - 06:05 AM

Short answer: Not supported.

IMO this would create more overhead than it's worth. The problem is not so much with creation but merely with updating related records. Simple example:

1. Author HAS_MANY Posts.
2. You load an author with all related post objects in an array ($author->posts).
3. You want to delete a single post for that author, so you remove it from the array $author->posts.
4. When you $author->save(), the removed post should now get deleted from db.

So the above means, whenever you call save(), AR would also have to re-check if one of the related objects has been removed. This can get complicated and time consuming very quickly.

As Yii tries to keep everything as slim and simple as possible i think it's perfectly o.k. to manually create related objects. That gives full control, there's only minimal extra code required and everything is pretty easy to understand.
0

#6 User is offline   omoratin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 23-November 09

Posted 24 November 2009 - 09:44 AM

Thanks, Mike.

Just wanted to know.

Do you know if I can use a transaction to save a bunch of objects from different models in a safe manner?
0

#7 User is offline   Mike 

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

Posted 24 November 2009 - 09:52 AM

Can't say for sure as i never used them. But i think yes, they do, as the transaction is created from the (shared) CDbConnection, not the model:
$transaction=$model->dbConnection->beginTransaction();

0

#8 User is offline   Vojtech 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 09-April 12
  • Location:Prague

Posted 25 June 2012 - 04:46 AM

I has been looking for the same functionality as well (for example LINQ in C# works in this way).

It seems that this extension may do the job:

http://www.yiiframew...ncedarbehavior/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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