Symfony - finding associated items, based on foreign key
I'm having a little bit of a struggle, trying to come up with a solution for the following:
I have a bunch of items, that are just content items, i.e. title etc I then can associate a tag to such items, in a many-to-many relationship
What I'm looking to do, is find all associated items, based on their tags.
Example: Item id 1 2 3 4
ItemTag
id | item_id | tag_id
1 | 1 | 1
2 | 1 | 2
3 | 2 | 2
4 | 4 | 3
5 | 4 | 4
Tag
id | tag
1 | Test_1
2 | Test_2
3 | Test_3
Now, wh开发者_JAVA技巧at I'm looking to do is, let's say i was on item 1, i need to find all associated items based on the item_tag
I am using Propel, so I'm looking for a criteria query...
Is this a good way of doing things?
I think something like this method (not tested) on Item should work:
public function getItemsWithSameTags()
{
$tags = $this->getTags();
$tagIDs = array();
foreach ($tags as $tag)
{
$tagIDs[] = $tag->getId();
}
return ItemQuery::create()
->useItemTagQuery()
->where('ItemTag.TagId IN ?', $tagIDs)
->endUse()
->find();
}
精彩评论