Part of the EllisLab Network
   
55 of 57
55
IgnitedRecord 1.0 pre-release
Posted: 11 May 2009 03:35 AM   [ Ignore ]   [ # 541 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  169
Joined  12-04-2008

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…

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

Profile
 
 
Posted: 11 May 2009 08:36 AM   [ Ignore ]   [ # 542 ]  
Grad Student
Avatar
Rank
Total Posts:  86
Joined  10-16-2008

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.

 Signature 

Beauty through Code.  Code through Knowledge. Knowledge through The Tubes.

Profile
 
 
Posted: 15 May 2009 10:30 AM   [ Ignore ]   [ # 543 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  740
Joined  08-03-2006

IgnitedRecord has support for the through relationship, but it is (as most parts in IR) undocumented.

I’ll write a quickie here:

// 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

 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser

Profile
 
 
Posted: 15 May 2009 12:01 PM   [ Ignore ]   [ # 544 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  169
Joined  12-04-2008

Thanks Tim & m4rw3r…

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

Profile
 
 
Posted: 19 May 2009 02:02 PM   [ Ignore ]   [ # 545 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  740
Joined  08-03-2006

Yeah, by using the through key in the config array, just as you specify special foreign key names.

 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser

Profile
 
 
Posted: 19 May 2009 02:14 PM   [ Ignore ]   [ # 546 ]  
Grad Student
Rank
Total Posts:  38
Joined  05-04-2009

That’s awesome

 Signature 

crainbandy.com

“Conditions are never perfect. ‘Someday’ is a disease that will take your dreams to the grave with you. If it’s important to you and you want to do it ‘eventually’, just do it and correct the course along the way.”
- Tim Ferriss

Profile
 
 
Posted: 06 June 2009 01:33 PM   [ Ignore ]   [ # 547 ]  
Summer Student
Total Posts:  3
Joined  04-06-2009

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.

Profile
 
 
Posted: 07 June 2009 06:56 PM   [ Ignore ]   [ # 548 ]  
Summer Student
Total Posts:  5
Joined  05-14-2009

I can’t seem to make the through relationship work the way I understand it should.  Here is my db layout:

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

Profile
 
 
Posted: 15 July 2009 10:39 PM   [ Ignore ]   [ # 549 ]  
Summer Student
Total Posts:  8
Joined  05-10-2007

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.

$faqs = $this->faq->find_all();

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

thanks!

Profile
 
 
Posted: 17 July 2009 11:42 AM   [ Ignore ]   [ # 550 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  740
Joined  08-03-2006

Your method is a good one, it is performing well. But it has a drawback, it can only extract data from saved objects, and the __data array will only be updated on save.

You can instead use the native method IR_Record::get_data() to get the data of the object in an array format.

 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser

Profile
 
 
   
55 of 57
55
 
‹‹ It worked      Extension to the Table Class ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120364 Total Logged-in Users: 32
Total Topics: 126489 Total Anonymous Users: 6
Total Replies: 665191 Total Guests: 314
Total Posts: 791680    
Members ( View Memberlist )