Welcome Guest, Not a member yet? Register   Sign In
ORM datamapper: shorthand for a simple many-to-many relationship
#1

[eluser]goddestroyer[/eluser]
I have developed at least 2 projects using the datamapper and i love it! however, i think this might be my first situation where I have a many-to-many relationship, and i'm looking for the most efficient way to ask for related objects....

the documentation shows that when you have something common like a many-to-one relationship of users to a group, you can use:
Code:
$user->group->get();
to get a handle on the Group object.
(where $user is a User model that has_one Group model).

Now, I have Entries, which can belong to multiple categories. I want to write a loop that just says (pseudocode):
Code:
$categories = new Category();
$categories->get();
foreach($categories as $category){
   $entries = $category->entries->get();
   ...
}
I can't seem to match any of the syntax of the example documentation up to this situation, i know it is right there but i'm not seeing it. What is the best way to get all entries in a category, where entries and categories have a many-to-many relationship?

thank you
#2

[eluser]goddestroyer[/eluser]
oh, and inside the loop I also tried:

Code:
$entries = new Entry();
$entries->where_related($category)->get();

but the error I get is hilarious:

Quote:Table 'categories_entries' doesn't exist

Which is funny, because it does exist, its called entries_categories.
#3

[eluser]Basketcasesoftware[/eluser]
Just caught this. I'm on my way to bed at the moment, but if wanwizard or someone else doesn't get to it first I'll see if I can.
#4

[eluser]WanWizard[/eluser]
@goddestroyer,

Then you haven't followed the guidelines in the documentation. Relationship tables are named using the plural of both models, in alphabetical order. Your table should be named 'categories_entries', as the c is before the e!
#5

[eluser]Basketcasesoftware[/eluser]
I'm up later than I should, but when I a reply given notice in my email I just had to confirm it was who I thougt it was. Smile I was right. Do any of us ever sleep around here? :gulp:
#6

[eluser]WanWizard[/eluser]
Sleep? What's that? Smile
#7

[eluser]goddestroyer[/eluser]
Thanks! thats just the heads up i needed. i think i was accidentally always following that rule with lookup tables in the past, and this was the first time i subconsciously violated it. Thanks, that works.




Theme © iAndrew 2016 - Forum software by © MyBB