Stable Solution for Saving Relational Active record.

(Codesutras) #1

Hello Everyone,

Well Last Night i was facing a very common problem of saving a HAS_MANY and MANY_MANY relation in Into the database.But after looking at serval forum post and documentation I come up with the point is that There is no such a better way to saving a HAS_MANY and MANY_MANY relation Active record into the Yii.

I know very well process of Collecting a tabular input to save has many relation.But this one is not a perfect OOP approach to resolve this problem.

I have checked Lots of Forum Post which are suggesting to use some kind of contributed Behavior Extensions.But its not a good thing to Depended on any contributed extension for this most usable and common task in Web environment.

Since, i have spend my lots of time with CakePHP also.Which is a good competitor against Yii.And CakePHP provide a perfect solution to perform a CRUD operation on Relational Active record.Defining a HAS_MANY relation in model class in Table A which have a has many relation with Table B and Calling a Save method on Model A save this relation work like a charm in CakePHP and also work perfectly on Updating and Deleting a Active record.

But I love Yii and It is my first choice to any kind of porject in it :) That is why i expecting this common feature should be included in Yii upcoming version to make it more Strong. :)

If by any chance there is a perfect solution available here in Yii.Then please clear my doubts :) Coz still i am in learning phase so might be i have done any mistake to understand this concept with Yii :P

Or Any body want to look what i am expecting which is already available in Cakephp then you can check it on This link.

But one thing i can assure you.If this kind of perfect solution will available Yii then Its going to be very easy for the users who are extensively using this framework. :)

So, I an requesting to the Yii Dev Staff to think about this Feature and expecting that it would be included in Upcoming version of Yii very soon. :)

Thanks to Yii Dev staff for making this powerfull framework.:)

Thanks to everyone here.

(Alexander Makarov) #2

This feature is planned for Yii2. Meanwhile you can use

(Ibo S) #3

Is there an approach how to implement deletion of related records ?

Sometimes you want to delete related records directly, sometimes you want not to delete, sometimes you want do delete if related record has no relations to other records (in case of many to many).

Do you think this Behavior could work with multi table inheritance ? For example with this one ?

(Alexander Makarov) #4

Deletion isn’t implemented in this behavior at all and I think it’s better to be done manually.

Never tried this behavior with any non-standard AR stuff so don’t know.

(Codesutras) #5

Thanks samdark, For your information :)

(Mobidevpublisher) #6

Looking forward for yii2, this would be a useful feature

(Rasmus) #7

It’s crazy how many third-party extensions/behaviors have been written to do this.

Saving related records is not weird or exotic behavior - it seems crazy that there still isn’t even a method on CActiveRecord that enables you to at least manually save has-many or many-to-many references.

Behaviors that automatically perform cascading saves don’t actually do it for me - I don’t mind having to write one line of code in the controller, or in afterSave(), to update a relation, because that line documents what’s happening, and makes it optional.

So I’m working on that now - methods that allow you to save related records with one call, e.g. $foo->saveRelated(‘bars’).

Where some AR implementations (and ORMs in general) get it completely wrong, in my opinion, is when they make no distinction between child records that are part of a parent aggregate, and related records that just happen to be referenced by a parent. AR (and OOP in general) tends to hide the distinction or forget that it exists.

To give a practical example, let’s say we have Users and Addresses with a one-to-many relationship. Addresses really “belong to” Users in this case (assuming that two Users can’t share the same Address) - in this case, you would probably want the User’s Address records to automatically save when you save the User.

Now consider a second relationship - another one-to-many relationship between Users and Countries. The same Countries of course are going to be referenced by many different Users - the Countries do not by any means “belong to” Users in this case, they’re just “borrowing” them. By no means would you want the actual Country objects to automatically save when you save a User - that wouldn’t make any sense.

As DDD people will point out, modeling this as $user->country in the first place, is misleading - the Country itself is not part of the User-aggregate, only the Country ID is actually part of that aggregate. Yet, we model it exactly the same way as we model $user->addresses, making no distinction between “owned” and “borrowed” objects. From an API perspective, there’s nothing indicating to the consumer of the model what will be saved and what won’t - the DDD camp rejects this idea, and models it as $user->country_id instead, to be clear about the fact that only the ID, not the object itself, is part of the User aggregate.

Whether you agree with that approach or not, the distinction needs to be made, and preferably in a way that is clear to the consumer. Hidden inside a bunch of configuration or behaviors, it’s not going to be obvious where the transaction boundaries are - how much or how little gets saved when you call save().

Since AR implementations at large tend to model the entire domain as an enormous graph that you can walk using lazy loading, I don’t mind keeping to that principle. But I hope to see Yii 2 implement loading and (especially) saving in a way that makes it obvious where the transaction boundaries lie.

(Stuart) #8

Using any of the extensions to provide many-many saves, I still cannot get any of them to save to my middle/mapping table, e.g:

Tables: user (id, name), project (id, name), map_user_project (user_id, project_id). All primary and foreign keys set properly, relations configured in my models etc.

I cannot save a project and have user_id and project_id inserted into the mapping table! Without writing the insert query myself of course. I expected Yii to support this “out the box” but it doesn’t, and as I say none of the extensions provide this either it seems.

I am new to Yii but well versed in php, can someone explain this to me please!?

(Ankit) #9

Hi samdark,

Is there any feature to save data in one-one relation. Like i have a User table and User Detail table liked with User_id.

Is there any way so i can insert record in both table with one save query.

I got a solution from another post but its not working:

$customer = new Customer;

$customer->name = ‘Qiang Xue’;

$customer->populateRelation(‘orders’, array(

new Order(array('type' => 'strong coffee')),

new Order(array('type' => 'tasty pizza')),


$customer->populateRelation(‘country’, array(

new Country(array('name' => 'United States')),


$customer->with(‘orders’, ‘country’)->validate();

Does Yii2 provide this functionality.

Anyone can answer on this…?????

(Vishwasrao Salunkhe) #10


Please try this:yii2-dynamicform from wbraganca

Its very good for relations.

I am currently using it in my project for invoice & invoiceitem table with one invoice can have many invoiceitem…