FabienPennequin / FPNTagBundle

This bundle allows to tag your Doctrine entities easily
http://knpbundles.com/FabienPennequin/FPNTagBundle
76 stars 50 forks source link

how to search tagged types by tags? #28

Closed waltercruz closed 9 years ago

waltercruz commented 10 years ago

Suppose that I have to search tagged types by 2 or 3 tags... How should I do?

And... the column resource_id should be integer don't?

pauletienney commented 10 years ago

I am interested too.

jbouzekri commented 10 years ago

Sorry I didn't understand your question. What do you mean by tagged types ?

lakiboy commented 10 years ago

I think he means how to search entity by 2+ matching tags i.e. find all items with tag "foo" and "bar".

You can do something like this:

// $dqlAlias - alias of your root entity
// $taggableType - desired taggable type
// $qb - your query builder 
// $data - list of tags

$qb->setParameter('tag_resource_type', $taggableType);

foreach ($data as $index => $tag) {
    $tagsQb = $this->tagManager->getRepository()->createQueryBuilder('tag' . $index)
        ->join("tag{$index}.tagging", 'tagging' . $index)
        ->where("tagging{$index}.resourceType = :tag_resource_type")
    ;
    $subQuery = $tagsQb
        ->andWhere($tagsQb->expr()->eq("tag{$index}.name", ':tag_name' . $index))
        ->select("tagging{$index}.resourceId")
        ->getDql()
    ;

    $qb->andWhere($qb->expr()->in($dqlAlias . '.id', $subQuery));

    /** @var \DoctrineExtensions\Taggable\Entity\Tag $tag */
    $qb->setParameter('tag_name' . $index, $tag->getName());
}

Will not comment on performance here.

hth