Kohana 3 ORM: How to get data from pivot table? and all other tables for that matter
Posted
by zenna
on Stack Overflow
See other posts from Stack Overflow
or by zenna
Published on 2010-05-23T20:53:13Z
Indexed on
2010/05/23
21:01 UTC
Read the original article
Hit count: 433
I am trying to use ORM to access data stored, in three mysql tables 'users', 'items', and a pivot table for the many-many relationship: 'user_item'
I followed the guidance from http://stackoverflow.com/questions/1946357/kohana-3-orm-read-additional-columns-in-pivot-tables
and tried
$user = ORM::factory('user',1);
$user->items->find_all();
$user_item = ORM::factory('user_item', array('user_id' => $user, 'item_id' => $user->items));
if ($user_item->loaded()) {
foreach ($user_item as $pivot) {
print_r($pivot);
}
}
But I get the SQL error:
"Unknown column 'user_item.id' in 'order clause' [ SELECT
user_item
.* FROMuser_item
WHEREuser_id
= '1' ANDitem_id
= '' ORDER BYuser_item
.id
ASC LIMIT 1 ]"
Which is clearly erroneous because Kohana is trying to order the elements by a column which doesn't exist: user_item.id. This id doesnt exist because the primary keys of this pivot table are the foreign keys of the two other tables, 'users' and 'items'.
Trying to use:
$user_item = ORM::factory('user_item', array('user_id' => $user, 'item_id' => $user->items))
->order_by('item_id', 'ASC');
Makes no difference, as it seems the order_by()
or any sql queries are ignored if the second argument of the factory is given.
Another obvious error with that query is that the item_id = '', when it should contain all the elements.
So my question is how can I get access to the data stored in the pivot table, and actually how can I get access to the all items held by a particular user as I even had problems with that?
Thanks
© Stack Overflow or respective owner