开发者

Getting all the associated tags for every item after a filterby(tag)

开发者 https://www.devze.com 2023-03-30 21:02 出处:网络
I have the typical association HABTM Item <-> Tag If i get all the Items with tag= \"test\" doing this:

I have the typical association HABTM Item <-> Tag

If i get all the Items with tag= "test" doing this:

$items = $this->Item->Tag->find('all', array('conditions'=>array('Tag.name'=>$tag)开发者_如何学Python));

The $items array shows correctly the [Items] array containing the Items associated with the tag, but How can i get also all the Tags for every item, so I can show after the item and all his tags ?

Thank you.


Edit: Seems like Cake didn't like the idea. You can hack it by calling the association a little differently:

$this->Item->bindModel(array('hasAndBelongsToMany' => array(
        'BlahTag'=>array(
            'className' => 'Tag',
            'joinTable' => 'items_tags',
            'foreignKey' => 'item_id',
            'associationForeignKey' => 'tag_id',
            'unique' => true,
        )
    )),false);
  $this->Item->BlahTag->bindModel(array('hasAndBelongsToMany' => array(
        'Item'=>array(
            'className' => 'Item',
            'joinTable' => 'items_tags',
            'foreignKey' => 'tag_id',
            'associationForeignKey' => 'item_id',
            'unique' => true,
        )
    )),false);
 $items = $this->Item->BlahTag->find('all', array(
   'contain'=>array('Item'=>array('BlahTag')),
   'conditions'=>array('BlahTag.name'=>$tag)
));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号