views:

254

answers:

1

Hi Guys,

I've got two tables (items / tags). Item has and belongs to many tags - tag has and belongs to many items.

It's no problem for me to fetch all related tags like:

$item = ORM::factory('item', 4);
foreach($item->tags as $tag){....}

But how can i fetch only one... and maybe a specific one?

Thanks in advance!

+1  A: 

In Kohana 3 you can do this:

$item = ORM::factory('item', 4);
$tag = $item->tags->where('somevalue','=',$value)->find();
if($tag->loaded()) {...}

Inside where() you put the condition you want.

Edit:

I did a little research and in Kohana 2.3.x the where syntax is different, instead of where('somevalue', '=', $value) you should write where('somevalue', $value). Or where('somevalue >', $value), where('somevalue !=', $value), etc.

dusan
thank you for your help. however i'm not sure what i'm doing wrong but it gives me following error message. it seems the tables are not inner joined. "There was an SQL error: Unknown column 'item_id' in 'where clause' - SELECT `tags`.* FROM (`tags`) WHERE `item_id` = '=' ORDER BY `tags`.`name` ASC LIMIT 0, 1" i'm using btw. kohana v2.3.4
n00b
Maybe it was my mistake, I thought that tag `belongs_to` item. I'll edit the answer.
dusan
do you happen to know how to do this in kohana 2.3.x?
n00b
What error are you getting?
dusan
still the error i mentioned above: There was an SQL error: Unknown column 'item_id' in 'where clause' - SELECT tags.* FROM (tags) WHERE item_id = '=' ORDER BY tags.name ASC LIMIT 0, 1"
n00b
Maybe you are calling `where('somevalue', '=', $value)` instead of `where('somevalue', $value)`. Check my edit.
dusan