Question

I have problem with Like in Query Builder:

I have SyntaxError:

[Syntax Error] line 0, col 243: Error: Expected '.' or '(', got 'type'


QueryException: SELECT z as task, GROUP_CONCAT(DISTINCT o.tahoObject) as type FROM Cloud\CrmBundle\Entity\Tasks z LEFT JOIN z.taskOwner u LEFT JOIN z.taskTasksHasObject o WHERE z.taskDone = :ztaskDone AND u.userFirstnameLastname = :uuserFirstnameLastname AND type LIKE :type GROUP BY z.taskId ORDER BY z.taskDate desc 

Im adding like to the Query Builder like that:

$qb->andWhere($filter['field'][0].' LIKE :' :type);
$qb->setParameter('type', '%'.$eachOption.'%');

I checked by 'getParameters()' that type paremeter is given properly so I don't understand why I have this syntax error... Group_concat works fine too, the problem is only when i'm adding like... Any ideas ?

Was it helpful?

Solution

This is the query you currently have -- it is erroring because in the WHERE clause it does not know a field named type:

SELECT 
    z as task,
    GROUP_CONCAT(DISTINCT o.tahoObject) as type
FROM Cloud\CrmBundle\Entity\Tasks z
    LEFT JOIN z.taskOwner u
    LEFT JOIN z.taskTasksHasObject o
WHERE z.taskDone = :ztaskDone
    AND u.userFirstnameLastname = :uuserFirstnameLastname
    AND type LIKE :type
GROUP BY z.taskId
ORDER BY z.taskDate desc

To fix this you need to restructure your query either this way by using GROUP_CONCAT in the where clause as well (but I am unsure if you can do the DISTINCT inside of the GROUP_CONCAT in the where clause - you can try it):

SELECT 
    z.taskId as task,   -- z as task did not make sense to me 
    GROUP_CONCAT(DISTINCT o.tahoObject) as type
FROM Cloud\CrmBundle\Entity\Tasks z
    LEFT JOIN z.taskOwner u   -- ON ... is missing
    LEFT JOIN z.taskTasksHasObject o   -- ON ... is missing 
WHERE z.taskDone = :ztaskDone
    AND u.userFirstnameLastname = :uuserFirstnameLastname
    AND GROUP_CONCAT(DISTINCT o.tahoObject) LIKE :type
GROUP BY z.taskId
ORDER BY z.taskDate desc

Or you may need to do a nested query:

SELECT task, type
FROM (
    SELECT 
        z.taskId as task,   -- z as task did not make sense to me 
        GROUP_CONCAT(DISTINCT o.tahoObject) as type
    FROM Cloud\CrmBundle\Entity\Tasks z
        LEFT JOIN z.taskOwner u   -- ON ... is missing
        LEFT JOIN z.taskTasksHasObject o   -- ON ... is missing
    WHERE z.taskDone = :ztaskDone
        AND u.userFirstnameLastname = :uuserFirstnameLastname
    GROUP BY z.taskId
    ORDER BY z.taskDate desc
) x
WHERE x.type LIKE :type

I would also like to point out that you appear to be missing join conditions on both your LEFT JOINS. Also, selecting z as task does not make sense to me. Perhaps you meant z.taskId as task? Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top