I have a problem with creating a certain query covering three different entities:
- Recipe - Ingredient - RecipeIngredient
A recipe and an ingredient both have many recipe ingredients - both relations are bidirectional.
In PHP I have an array with multiple ingredient ids and I want to fetch only those recipes having ALL of these ingredients.
In the following tblIngredients means the list of recipe ingredients seen from the recipe side.
First I tried something like: if (!empty($ingredients)) { $ingredientIds = explode('-', $ingredients); foreach ($ingredientIds as $ingredientId) { $qb->andWhere('tblIngredients.ingredient = :ingredientId') ->setParameter('ingredientId', $ingredientId); } } But this did not work because there isn't any single ingredient having all the given ingredient ids. Therefore the result was always empty.
My next attempt was: if (!empty($ingredients)) { $ingredientIds = explode('-', $ingredients); foreach ($ingredientIds as $ingredientId) { $qb->andWhere(':ingredientId MEMBER OF tblIngredients.ingredient') ->setParameter('ingredientId', $ingredientId); } } which caused an error because tblIngredients.ingredient is not multi-valued.
> I have a problem with creating a certain query covering three different
> entities:
> - Recipe
> - Ingredient
> - RecipeIngredient
> A recipe and an ingredient both have many recipe ingredients - both
> relations are bidirectional.
> In PHP I have an array with multiple ingredient ids and I want to fetch
> only those recipes having ALL of these ingredients.
> In the following tblIngredients means the list of recipe ingredients seen
> from the recipe side.
> First I tried something like:
> if (!empty($ingredients)) {
> $ingredientIds = explode('-', $ingredients);
> foreach ($ingredientIds as $ingredientId) {
> $qb->andWhere('tblIngredients.ingredient = :ingredientId')
> ->setParameter('ingredientId', $ingredientId);
> }
> }
> But this did not work because there isn't any single ingredient having all
> the given ingredient ids. Therefore the result was always empty.
> My next attempt was:
> if (!empty($ingredients)) {
> $ingredientIds = explode('-', $ingredients);
> foreach ($ingredientIds as $ingredientId) {
> $qb->andWhere(':ingredientId MEMBER OF
> tblIngredients.ingredient')
> ->setParameter('ingredientId', $ingredientId);
> }
> }
> which caused an error because tblIngredients.ingredient is not
> multi-valued.
> Can anybody help me with this challenge, please?
> I am using Doctrine 2.1.5.
> TIA,
> Axel
> --
> You received this message because you are subscribed to the Google Groups
> "doctrine-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/doctrine-user/-/lVkz-xUFXIcJ.
> To post to this group, send email to doctrine-user@googlegroups.com.
> To unsubscribe from this group, send email to
> doctrine-user+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/doctrine-user?hl=en.
> On 3 June 2012 13:06, Guite <guitezik...@googlemail.com> wrote:
>> Hello,
>> I have a problem with creating a certain query covering three different
>> entities:
>> - Recipe
>> - Ingredient
>> - RecipeIngredient
>> A recipe and an ingredient both have many recipe ingredients - both
>> relations are bidirectional.
>> In PHP I have an array with multiple ingredient ids and I want to fetch
>> only those recipes having ALL of these ingredients.
>> In the following tblIngredients means the list of recipe ingredients seen
>> from the recipe side.
>> First I tried something like:
>> if (!empty($ingredients)) {
>> $ingredientIds = explode('-', $ingredients);
>> foreach ($ingredientIds as $ingredientId) {
>> $qb->andWhere('tblIngredients.ingredient = :ingredientId')
>> ->setParameter('ingredientId', $ingredientId);
>> }
>> }
>> But this did not work because there isn't any single ingredient having
>> all the given ingredient ids. Therefore the result was always empty.
>> My next attempt was:
>> if (!empty($ingredients)) {
>> $ingredientIds = explode('-', $ingredients);
>> foreach ($ingredientIds as $ingredientId) {
>> $qb->andWhere(':ingredientId MEMBER OF
>> tblIngredients.ingredient')
>> ->setParameter('ingredientId', $ingredientId);
>> }
>> }
>> which caused an error because tblIngredients.ingredient is not
>> multi-valued.
>> Can anybody help me with this challenge, please?
>> I am using Doctrine 2.1.5.
>> TIA,
>> Axel
>> --
>> You received this message because you are subscribed to the Google Groups
>> "doctrine-user" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/doctrine-user/-/lVkz-xUFXIcJ.
>> To post to this group, send email to doctrine-user@googlegroups.com.
>> To unsubscribe from this group, send email to
>> doctrine-user+unsubscribe@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/doctrine-user?hl=en.
> --
> You received this message because you are subscribed to the Google Groups
> "doctrine-user" group.
> To post to this group, send email to doctrine-user@googlegroups.com.
> To unsubscribe from this group, send email to
> doctrine-user+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/doctrine-user?hl=en.
-- Guilherme Blanco
MSN: guilhermebla...@hotmail.com
GTalk: guilhermeblanco
Toronto - ON/Canada
Not really a solution, but did you check the generated SQL? It seems that
in your first 2 attempts you re-use a named parameter, which probably
overwrites the values...
Third attempt seems right, will check some code of mine on Monday, problem
sounds familiar...
On Jun 3, 2012 8:06 AM, "Guite" <guitezik...@googlemail.com> wrote:
> I have a problem with creating a certain query covering three different
> entities:
> - Recipe
> - Ingredient
> - RecipeIngredient
> A recipe and an ingredient both have many recipe ingredients - both
> relations are bidirectional.
> In PHP I have an array with multiple ingredient ids and I want to fetch
> only those recipes having ALL of these ingredients.
> In the following tblIngredients means the list of recipe ingredients seen
> from the recipe side.
> First I tried something like:
> if (!empty($ingredients)) {
> $ingredientIds = explode('-', $ingredients);
> foreach ($ingredientIds as $ingredientId) {
> $qb->andWhere('tblIngredients.ingredient = :ingredientId')
> ->setParameter('ingredientId', $ingredientId);
> }
> }
> But this did not work because there isn't any single ingredient having all
> the given ingredient ids. Therefore the result was always empty.
> My next attempt was:
> if (!empty($ingredients)) {
> $ingredientIds = explode('-', $ingredients);
> foreach ($ingredientIds as $ingredientId) {
> $qb->andWhere(':ingredientId MEMBER OF
> tblIngredients.ingredient')
> ->setParameter('ingredientId', $ingredientId);
> }
> }
> which caused an error because tblIngredients.ingredient is not
> multi-valued.
> Can anybody help me with this challenge, please?
> I am using Doctrine 2.1.5.
> TIA,
> Axel
> --
> You received this message because you are subscribed to the Google Groups
> "doctrine-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/doctrine-user/-/lVkz-xUFXIcJ.
> To post to this group, send email to doctrine-user@googlegroups.com.
> To unsubscribe from this group, send email to
> doctrine-user+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/doctrine-user?hl=en.