IgnitedRecord 1.0 pre-release - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: IgnitedRecord 1.0 pre-release (/showthread.php?tid=7996) |
IgnitedRecord 1.0 pre-release - El Forum - 05-08-2009 [eluser]Tim Stackhouse[/eluser] This may or may not be an issue, but it could be a useful reference for other coders: I have a table that has 2 foreign key constraints that depend on two other tables. Without resorting to $obj->fk_1 = $obj2->id; $obj->add('obj3', $obj3); there is no way to use add() to do both since it saves after adding the relation. Is it contrary to the design of IR to do $obj->fk_1 = $obj2->id; as opposed to using $obj->add() when dealing with multiple constraints on the database table? IgnitedRecord 1.0 pre-release - El Forum - 05-11-2009 [eluser]helmutbjorg[/eluser] Hi there.. Was just wondering if there was a way to store any extra data in a habtm relationship? For example with the following tables... Code: users When using the $user->add('courses', $course) method and the $user->related('courses')->get() method it would be great to be able to get at the date_enrolled field in the linking table? Is there a way to do it that I am missing? I know your script can't (and shouldn't) handle every possible situation but i was just wondering as this has come up in a project. Thanks Steve IgnitedRecord 1.0 pre-release - El Forum - 05-11-2009 [eluser]Tim Stackhouse[/eluser] To my knowledge there are two ways you could go about this: The first method, I'm not sure if IR supports it, would be a through relationship, like in Rails, however I don't believe it is supported in IR, but I'll defer to the authority for that answer. The way I would do it involves converting courses_users into its own model, say enrollments. You could than define that users has_many enrollments, courses has_many enrollments and enrollments belongs_to user, course. Doing it this way, you're effectively emulating a habtm through relationship using a pair of has_manys. IgnitedRecord 1.0 pre-release - El Forum - 05-15-2009 [eluser]m4rw3r[/eluser] IgnitedRecord has support for the through relationship, but it is (as most parts in IR) undocumented. I'll write a quickie here: Code: // use has one or has many IgnitedRecord 1.0 pre-release - El Forum - 05-15-2009 [eluser]helmutbjorg[/eluser] Thanks Tim & m4rw3r... One question for m4rw3r... can you define the through relationship in the model? IgnitedRecord 1.0 pre-release - El Forum - 05-19-2009 [eluser]m4rw3r[/eluser] Yeah, by using the through key in the config array, just as you specify special foreign key names. IgnitedRecord 1.0 pre-release - El Forum - 05-19-2009 [eluser]tekhneek[/eluser] That's awesome IgnitedRecord 1.0 pre-release - El Forum - 06-06-2009 [eluser]rayray[/eluser] Opinion: Black Buddha brought up something a while back which I just ran into--any record with multiple belongs_to relationships will throw foreign key constraint errors when you try to do something like: $object->add('some_belongs_to_relation',$another_object); $object->add('some_other_belongs_to_relation',$yet_another_object); While automatically saving all objects of the relation upon calling add seems like a nice feature, it causes problems working with complex relationship models. I'd vote for changing add relations without saving as the default behavior and only save when you explicitly call save on one of the objects in the relation ship. Buddha's work around by changing the explicit save() does work, but I prefer not to deviate others' libraries. Currently, I work around the issue by manually building all but the last belongs_to relationship like: $object->another_object_id = $another_object->id; $object->add('some_other_belongs_to_relation',$yet_another_object); The only issue with this approach is it sort of breaks the elegant ORM abstraction and if I change a belongs_to relationship into say a 'has_one' or habtm, my code breaks. IgnitedRecord 1.0 pre-release - El Forum - 06-07-2009 [eluser]dulakian[/eluser] I can't seem to make the through relationship work the way I understand it should. Here is my db layout: Code: Users User has many pages Page has many polls Poll has one Question Polls habtm Answers User has many polls through pages My first question is do I have to reverse the through relationship? Meaning in the Poll model do I have to set a belongs_to relation to Users, or something similar? I'm a little unclear how I would reverse the through relation, if that's even necessary here. You didn't do a reverse on your example, but you were using the factory instead of the model as I am... When I try to access the related polls from the user model, it expects polls to have a user_id field, is this normal? I thought it would relate through the user_id in Pages, not a field in Polls. If I have to add a user_id field to Polls, it seems to defeat the purpose of the through relationship imo, I could just set up a has_many from users to polls, and has_many from pages to polls. The habtm relation from polls to Questions and Answers works perfectly, I can pull an entire poll with a single query, which is exactly how I want it (using 2 join_related commands to put the Question and all Answers together). I'd like to get all polls, with Question and Answers, by user_id, and thought the through relationship would work for this. I think I'm missing something simple here but don't see it... Thanks for such a great library, it really rocks not to have to code out all the relationships by hand. Michael IgnitedRecord 1.0 pre-release - El Forum - 07-15-2009 [eluser]tpchris[/eluser] is there a method for converting the results of a find_all() or any other retrieval method for that matter to an array similar to the CI db result_array()? I am using Smarty which doesn't know how to handle an IR_record Object so I need to convert it to a array. I came up with this but am not sure its the most efficient and wanted to check if there was something native to the framework. Code: $faqs = $this->faq->find_all(); |