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
Page 1 of 1
Save and related record
#2
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~
$post = Post::model()->findByPk(10);
$post->author->name = 'Jerry';
$post->save();
$post->author->name is changed to 'Jerry' ?
not supported seemingly~
#3
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
$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
#5
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.
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.
#6
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?
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?
#7
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();
#8
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/
It seems that this extension may do the job:
http://www.yiiframew...ncedarbehavior/
Share this topic:
Page 1 of 1

Help
This topic is locked













