Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord 1.0 pre-release

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

[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
- id
- name
- address

courses
- id
- title
- description

courses_users
- id
- course_id
- user_id
- date_enrolled

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

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

[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
// then use it like if you relate from the related table to the table you relate through to

$group = IgnitedRecord::factory('groups');
$group->has_many('users');

$user = IgnitedRecord::factory('users');
$user->has_many('posts');

$post = IgnitedRecord::factory('posts');
$user->has_many('posts');

// doesn't matter if you define this the last, but I do it here for clarity:
$group->has_many('posts')->through('users');

// then it assumes that posts is the relation name in the users model linking to the posts table
$posts = $group->find(1)->related('posts')->get();
Note: The details are a bit fussy, so excuse me if it isn't understandable

[eluser]helmutbjorg[/eluser]
Thanks Tim & m4rw3r...

One question for m4rw3r... can you define the through relationship in the model?

[eluser]m4rw3r[/eluser]
Yeah, by using the through key in the config array, just as you specify special foreign key names.

[eluser]tekhneek[/eluser]
That's awesome

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

[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
-id

Pages
-id
-user_id

Polls
-id
-page_id

Questions
-id
-poll_id

Polls_Answers
-id
-poll_id
-answer_id

Answers
-id
I've set my relationships like this:

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

[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();

$result_array = array();
foreach($faqs as $faq)
{
   array_push($result_array, $faq->__data);
}
thanks!




Theme © iAndrew 2016 - Forum software by © MyBB