Yii Framework Forum: Stable Solution for Saving Relational Active record. - Yii Framework Forum

Jump to content

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

Stable Solution for Saving Relational Active record. A common problem to save a relational active record Rate Topic: -----

#1 User is offline   codesutra 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 596
  • Joined: 15-March 11
  • Location:India

Posted 06 June 2012 - 12:18 AM

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.
CodeSutra
1

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,353
  • Joined: 17-January 09
  • Location:Russia

Posted 21 June 2012 - 09:58 AM

This feature is planned for Yii2. Meanwhile you can use http://yiiext.github...vior/index.html
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
1

#3 User is offline   ibo_s 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 19-July 11

Posted 14 July 2012 - 06:39 AM

View Postsamdark, on 21 June 2012 - 09:58 AM, said:

This feature is planned for Yii2. Meanwhile you can use http://yiiext.github...vior/index.html


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 ?
http://www.yiiframew...tance-approach/
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,353
  • Joined: 17-January 09
  • Location:Russia

Posted 14 July 2012 - 02:49 PM

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.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#5 User is offline   codesutra 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 596
  • Joined: 15-March 11
  • Location:India

Posted 23 July 2012 - 08:05 AM

View Postsamdark, on 21 June 2012 - 09:58 AM, said:

This feature is planned for Yii2. Meanwhile you can use http://yiiext.github...vior/index.html



Thanks samdark, For your information :)
CodeSutra
0

#6 User is offline   MobiDev Corporation 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 10-June 12

Posted 28 July 2012 - 06:48 AM

Looking forward for yii2, this would be a useful feature
0

#7 User is offline   mindplay 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 397
  • Joined: 03-September 09
  • Location:New York

Posted 24 January 2013 - 08:09 PM

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

#8 User is offline   Stu 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-February 13

Posted 08 February 2013 - 09:00 AM

View Postsamdark, on 21 June 2012 - 09:58 AM, said:

This feature is planned for Yii2. Meanwhile you can use yiiext.github.com/extensions/with-related-behavior/index.html


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!?
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