Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Filtering problem
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Guite  
View profile  
 More options Jun 3 2012, 7:06 am
From: Guite <guitezik...@googlemail.com>
Date: Sun, 3 Jun 2012 04:06:46 -0700 (PDT)
Local: Sun, Jun 3 2012 7:06 am
Subject: Filtering problem

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.

So I tried this:
        if (!empty($ingredients)) {
            $ingredientIds = explode('-', $ingredients);
            $i = 0;
            foreach ($ingredientIds as $ingredientId) {
                $i++;
                $qb2 = $this->getEntityManager()->createQueryBuilder();
                $qb2->select('tblRecipeIngredients' . $i . '.ingredient')
                    ->from('MyEntity_RecipeIngredient',
'tblRecipeIngredients' . $i)
                    ->where('tblRecipeIngredients' . $i . '.ingredient =
:ingredientId')
                    ->setParameter('ingredientId', $ingredientId)
                    ->andWhere('tblRecipeIngredients' . $i . '.recipe =
tbl.id');
                $qb->andWhere($qb->expr()->exists($qb2->getDql()));
            }
        }

which gives me another error:

[Semantical Error] line 0, col 280 near 'ingredient FROM': Error: Invalid
PathExpression. Must be a StateFieldPathExpression.

   - #0 Exception in
   /var/customers/webs/GuiteLab/multiCMS/plugins/Doctrine/lib/vendor/Doctrine/ ORM/Query/QueryException.php,
   Zeile 47
   - #1 /var/.../Doctrine/ORM/Query/Parser.php(717):
   Doctrine\ORM\Query\Parser->semanticalError('Invalid PathExp...', Array)
   - #2 /var/.../Doctrine/ORM/Query/Parser.php(221):
   Doctrine\ORM\Query\Parser->_processDeferredPathExpressions(Object(Doctrine\ ORM\Query\AST\SelectStatement))
   - #3 /var/.../Doctrine/ORM/Query/Parser.php(281):
   Doctrine\ORM\Query\Parser->getAST()
   - #4 /var/.../Doctrine/ORM/Query.php(213):
   Doctrine\ORM\Query\Parser->parse()
   - #5 /var/.../Doctrine/ORM/Query.php(234): Doctrine\ORM\Query->_parse()
   - #6 /var/.../Doctrine/ORM/AbstractQuery.php(607):
   Doctrine\ORM\Query->_doExecute()
   - #7 /var/.../Doctrine/ORM/AbstractQuery.php(480):
   Doctrine\ORM\AbstractQuery->execute(Array, 4)
   - #8 /var/.../Doctrine/ORM/AbstractQuery.php(506):
   Doctrine\ORM\AbstractQuery->getSingleResult(4)
   - #9
   /var/.../DoctrineExtensions/lib/DoctrineExtensions/Paginate/Paginate.php(40 ):
   Doctrine\ORM\AbstractQuery->getSingleScalarResult()

Can anybody help me with this challenge, please?

I am using Doctrine 2.1.5.

TIA,
Axel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Pivetta  
View profile  
 More options Jun 3 2012, 8:23 am
From: Marco Pivetta <ocram...@gmail.com>
Date: Sun, 3 Jun 2012 14:23:03 +0200
Local: Sun, Jun 3 2012 8:23 am
Subject: Re: [doctrine-user] Filtering problem

What is your "tblRecipeIngredients$i.ingredient"? Is it an association?

Anyway, the approach seems correct, even if it requires a lot of DQL (not
that I can think of an SQL solution right now).

Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com

On 3 June 2012 13:06, Guite <guitezik...@googlemail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
guilhermeblanco@gmail.com  
View profile  
 More options Jun 3 2012, 11:39 am
From: "guilhermebla...@gmail.com" <guilhermebla...@gmail.com>
Date: Sun, 3 Jun 2012 11:39:57 -0400
Local: Sun, Jun 3 2012 11:39 am
Subject: Re: [doctrine-user] Filtering problem

If you're able to upgrade to master, you can do:

IDENTITY(tblRecipeIngredients$i.ingredient)

Otherwise, you are forced to JOIN the ingredient and then grab its id.

--
Guilherme Blanco
MSN: guilhermebla...@hotmail.com
GTalk: guilhermeblanco
Toronto - ON/Canada

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Menno Holtkamp  
View profile  
 More options Jun 3 2012, 12:57 pm
From: Menno Holtkamp <menno.holtk...@gmail.com>
Date: Sun, 3 Jun 2012 13:57:52 -0300
Local: Sun, Jun 3 2012 12:57 pm
Subject: Re: [doctrine-user] Filtering problem

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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guite  
View profile  
 More options Jun 5 2012, 8:23 am
From: Guite <guitezik...@googlemail.com>
Date: Tue, 5 Jun 2012 05:23:12 -0700 (PDT)
Local: Tues, Jun 5 2012 8:23 am
Subject: Re: Filtering problem

Thank you all for your answers.

I got it working thanks to the hint from Guilherme Blanco that I must join
the ingredient table, too.

Here is how it works now:

        $qb = ...; // query builder for the recipe table with alias tbl

        if (!empty($ingredients)) {
            $ingredientIds = explode('-', $ingredients);
            $i = 0;
            foreach ($ingredientIds as $ingredientId) {
                $i++;
                $riTable = 'tblRecipeIngredients' . $i;
                $ingredientTable = 'tblIngredient' . $i;
                $qb2 = $this->getEntityManager()->createQueryBuilder();
                $qb2->select($ingredientTable . '.id')
                    ->from('Entity_RecipeIngredient', $riTable)
                    ->leftJoin($riTable . '.ingredient', $ingredientTable)
                    ->where($ingredientTable . '.id = ' . $ingredientId)
                    ->andWhere($riTable . '.recipe = tbl.id'); // tbl.id ==
recipe.id

                // add sub query to main query
                $qb->andWhere($qb->expr()->exists($qb2->getDql()));
            }
        }

Regards,
Axel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »