views:

31

answers:

1

I have a database with many-to-many relations on tag and person. I am in trouble when tried to search a person with multiple tags. I tried this but its fail:

        $person = Doctrine_Query::create()
        ->from("Model_Person p")
        ->innerJoin("p.tags t")
        ->whereIn('t.id',$t)
        ->execute();

The above statement return all persons that have at least one of the tags in array $t, but I want only person that havel 'all' the tags on that array.

Anyone know how to achieve this ?

Thanks

+1  A: 

This is because IN returns all results with at least one match. Say if you have id IN(1,2,3) it's the same as id = 1 OR id = 2 OR id = 3.

To archieve your desired goal, use Doctrine's andWhere() method with each value that is now in $t e.g. [...]->andWhere('id = ?', 1)->andWhere('id = ?', 2)->[...]

DrColossos