I have a query that output some very inconvenient error message:
Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException'
with message 'SELECT p, e, c FROM Entity\Event e LEFT JOIN e.place p
INNER JOIN e.categories c WITH c MEMBER OF :cat WHERE (e.dateStart
BETWEEN :from AND :to) AND (p.latitude BETWEEN :minLat AND :maxLat)
AND (p.latitude BETWEEN :minLat AND :maxLat) AND (p.latitude BETWEEN
:minLat AND :maxLat) AND (p.longitude BETWEEN :minLng AND :maxLng)
ORDER BY e.dateStart ASC' in
/Users/YohannM/Sites/meetmyfriends-back/application/libraries/Doctrine/ORM/Query/QueryException.php:39Stack
trace:#0
/Users/YohannM/Sites/meetmyfriends-back/application/libraries/Doctrine/ORM/Query/Parser.php(429):
Doctrine\ORM\Query\QueryException::dqlError('SELECT p, e, c ...')#1
/Users/YohannM/Sites/meetmyfriends-back/application/libraries/Doctrine/ORM/Query/Parser.php(528):
Doctrine\ORM\Query\Parser->semanticalError('':cat' is not d...',
Array)#2
/Users/YohannM/Sites/meetmyfriends-back/application/libraries/Doctrine/ORM/Query/Parser.php(233):
Doctrine\ORM\Query\Parser->_processDeferred in
/Users/YohannM/Sites/meetmyfriends-back/application/libraries/Doctrine/ORM/Query/QueryException.php
on line 49
As you can see, this is very inconvenient for debugging as my errors are truncated: Parser->semanticalError('':cat' is not d...',.
I have tried to vardump the excpetion but chrome crashes as the results returned is over 1GB !!
So my question is, how do I output the errors nicely.
I'm not working with symfony2 but with Codeingniter
Thanks
You should not use vardump to dump an exception. Many Doctrine classes are interlinked, so, when you try to vardump an exception, it creates a recursion, and that is why your browser exhausts available memory and crashes.
Doctrine has a utility that allows you to dump interlinked objects and specify the level of recursion. For example, to dump an object and all linked object up to 5 levels deep use this:
\Doctrine\Common\Util\Debug::dump($object, 5);
The default depth level is 2. More info - http://www.doctrine-project.org/api/common/2.4/class-Doctrine.Common.Util.Debug.html
Related
I'm trying to execute a (very long) query into an SQLite database using PHP (v8.0.5) PDO, but it seems like my query is being cut off when I call prepare().
Some snippets of my code are below:
$db = new SQLite3($SQLITE_DB_NAME);
$overall_query = 'SELECT DISTINCT presentations.Presentation_ID, presentations.Presentation_Name, presentations.Presentation_Type,
presentations.Presentation_File, presentations.Conference_ID,
GROUP_CONCAT(presentation_authors.Author_ID, ",") OVER
(PARTITION BY presentation_authors.Presentation_ID ORDER BY presentation_authors.Author_Position ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS Author_ID_List
FROM presentations
LEFT JOIN conferences ON conferences.Conference_ID=presentations.Conference_ID
LEFT JOIN presentation_authors ON presentation_authors.Presentation_ID=presentations.Presentation_ID
WHERE 1 LIMIT :query_lim;';
...
$overall_query_prep = $db->prepare($overall_query);
When I echo $overall_query; and print_r($overall_query);, I get the full query as expected, and the query works on my SQLite database. However, I get an warning from PHP that says: Warning: SQLite3::prepare(): Unable to prepare statement: 17, near "(": syntax error in file.php on line 223. This eventually results in a fatal error because it can't bind the values below.
Looking at the call stack in the warning, it looks like the variable was cut off when passing it to prepare. This is what the call stack shows (with ... to omit some repeated things): prepare( $query = 'SELECT DISTINCT ... ON conferences.Conference_ID=p' ). The maximum length of what it sees appears to be fixed, as I tried shortening the statement and I see more of the query.
Is this just a result of how the call stack is shown or has PHP cut off my query somewhere? If so, why is it cutting it off?
My PHP Code is:
$company = $qb->select("c")
->from("CRMBundle:TblCompanyDomain","cd")
->join("cd.company","c")
->leftJoin("CRMBundle:TblCompanyAddress",
"ca",
"WITH",
"ca.company = c")
->join("ca.country","cc")
->where($qb->expr()->andX(
$qb->expr()->in("cd.domain",":domain"),
$qb->expr()->eq("c.channel",":channel_id")
))
->setParameters($parameters)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
End Received DQL code is:
SELECT
c
FROM
CRMBundle:TblCompanyDomain cd
INNER JOIN cd.company c
LEFT JOIN CRMBundle:TblCompanyAddress ca WITH ca.company = c
INNER JOIN ca.country cc
WHERE
cd.domain IN(:domain) AND
c.channel = :channel_id
When I execute this query, I've received this error:
[Syntax Error] line 0, col 44: Error: Expected end of string, got 'I'
enter code here
I could not see any syntax error. Where is the problem?
My Symfony version: 2.6.11
My Doctrine Version: 2.2
SELECT
c
FROM
CRMBundle:TblCompanyDomain cd
INNER JOIN cd.company c
LEFT JOIN CRMBundle:TblCompanyAddress ca WITH ca.company = c
INNER JOIN ca.country cc
WHERE
cd.domain IN(:domain) AND
c.channel = :channel_id
I think that you need c.something
I've changed from annotation to yml my doctrine orm mapping and fixed problems.
I've experienced to many times same problem and understand real problem. When I using Doctrine annotation with utf-8, this problem fired. May be, cause is utf-8 on Turkish. Because lower "ı" charachter's capital is "I" and lower "i" carachter's capital is "İ".
Using symfony2/doctrine2, I have a hard time defining an index for my query.
My code :
$queryBuilder = $this->_em
->createQueryBuilder()
->select('u, uis, cost, p, stock')
->from('AppBundle:FoodAnalytics\UserIngredient', 'u', 'p.id')
->leftJoin('u.product', 'p')
->leftJoin('u.numberObjects', 'stock')
->leftJoin('u.userIngredientSuppliers', 'uis')
->leftJoin('uis.numberObjects', 'cost')
->where('u.user = ?1')
->setParameter(1, $portfolioUser)
;
I get the following error :
[Semantical Error] line 0, col 110 near 'p LEFT JOIN u.numberObjects': Error: 'p' is already defined.
500 Internal Server Error - QueryException
1 linked Exception: QueryException »
[1/2] QueryException: SELECT u, uis, cost, p, stock FROM AppBundle:FoodAnalytics\UserIngredient u INDEX BY p.id LEFT JOIN u.product p LEFT JOIN u.numberObjects stock LEFT JOIN u.userIngredientSuppliers uis LEFT JOIN uis.numberObjects cost WHERE u.user = ?1 +
Using u.product I get the following error :
[Semantical Error] line 0, col 87 near 'product LEFT': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
500 Internal Server Error - QueryException
1 linked Exception: QueryException »
Using just product, I get the following error:
[Semantical Error] line 0, col 85 near 'product LEFT': Error: 'product' does not point to a Class.
500 Internal Server Error - QueryException
1 linked Exception: QueryException »
It works fine if I use u.id
Can I only use index by a field from the same table ?
What can I do to make it work ?
Thansk a lot !
EDIT :
As a temporary fix I am using :
$result = $queryBuilder->getQuery()->getResult();
$result = array_combine(array_map(function(UserIngredient $userIngredient){
return $userIngredient->getProduct()->getId();
}, $result), $result);
return $result;
The attribute indexBy only apply to the entity you are currently selecting, as you have guessed (and AFAIK); so in your case you could only index by u.id.
This makes sense to me, since indexing from other entity really mess up the returned results. The only situation in which you'll get proper result is when:
all the join from the main entity and the "target" indexBy entity are one-to-one;
all the join are INNER JOIN;
the indexBy column of the resultset is unique.
In every other case you loose some instance reference during hydration.
In your example, you are using LEFT JOIN: what will happen to all entities without product? All discarded, so the LEFT JOIN would have worker like an INNER JOIN. Also, your workaround suggest that you want to combine entities with the same index, but this is not how Doctrine work: in Doctrine an indexBy column MUST be unique. See the official docs for more info.
Note that you could indexBy p.id in your JOIN clause, but this has different meaning. It means that when hydrating the UserIngredients instances, the collection of product should be indexed by id.
So, my suggestion is to follow one of this two solution:
give up on Doctrine indexBy, and use your workaraound;
If you have the inverse association Product -> UserIngredients, use Product as the main entity of the query (the from clause, and first to appear in select), and then index by p.id.
Which one to use depends on your business logic, but I generally prefer the second solution, since the first generate multiple-level array, which are better represented by entity relations.
Hope this help! :)
New to Doctrine, and trying to get a JOIN to work.
I've banged my head on this for awhile, and am slowly beginning to get the whole picture - its not as simply as I thought it would be.
I have two Entities with corresponding sql tables. I'm attempting to run a query that will return data from both of them, linked. A JOIN.
If I was doing this in straight SQL, it would look like this:
SELECT *
FROM mail_links a
INNER JOIN mail_msgs b
ON a.msg_id = b.id
I've tried a few things to make this work, including:
$query = $this -> doctrine -> em -> createQuery ("SELECT a FROM ORM\Dynasties2\Maillinks a JOIN ORM\Dynasties2\Mailmsgs b ON a.msgId = b.id");
That of course did not work. I realized that this relies on having Entities created as Classes, or at least that's what some examples seemed to be doing.
And that's where I'm completely lost.
Is it correct that I need those entities to be Classes? Are there any complete code examples of this (using CI) that would help me understand how to write the classes necessary for this JOIN?
Edit:
It just dawned on me that I DO have classes setup. That's what my entities ARE.
So how do I make this work:
$query = $this -> doctrine -> em -> createQuery ("SELECT a FROM ORM\Dynasties2\Maillinks a JOIN ORM\Dynasties2\Mailmsgs b ON a.msgId = b.id");
It gives the following error:
Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message '[Semantical Error] line 0, col 70 near 'b ON a.msgId': Error: Identification Variable ORM\Dynasties2\Mailmsgs used in join path expression but was not defined before.' in /PATH/applicationFolder/libraries/Doctrine/ORM/Query/QueryException.php:47 Stack trace: #0 /PATH/applicationFolder/libraries/Doctrine/ORM/Query/Parser.php(413): Doctrine\ORM\Query\QueryException::semanticalError('line 0, col 70 ...') #1 /PATH/applicationFolder/libraries/Doctrine/ORM/Query/Parser.php(914): Doctrine\ORM\Query\Parser->semanticalError('Identification ...') #2 /PATH/applicationFolder/libraries/Doctrine/ORM/Query/Parser.php(1567): Doctrine\ORM\Query\Parser->JoinAssociationPathExpression() #3 /PATH/ in /PATH/applicationFolder/libraries/Doctrine/ORM/Query/QueryException.php on line 47
My program generates this DQL using echo $q->getDql(); reformated for easier reading:
SELECT o.*, t.*, COUNT(t.id) AS num_of_reservations
FROM Property o
LEFT JOIN o.Reservation t
WITH t.status=? AND
( (t.start_date<=? AND t.end_date>=?)
OR (t.start_date>=? AND t.start_date <= ?) )
GROUP BY o.id
HAVING num_of_reservations < o.nr_of_bookings
Using PHP unit tests, I get results as expected. However, when I send this Doctrine_Query to Doctrine_Pager, I get this error:
Fatal error: Uncaught exception 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'p.nr_of_bookings' in 'having clause'
I tried all combinations like using having('COUNT(t.id)..... ) etc, different SELECT combos, all results picked by $q->execute() are good except when I send it to pager.
Can somebody help me with this? I even tried
FROM Property p
instead of
FROM Property o
no difference at all.