Symfony 1.4: Doctrine DQL Doesn’t Like Double Quotes

There are a number of problems that can cause this Doctrine error:

PHP Fatal error:  Call to a member function evictAll() on a non-object

One of them relates to using double quotes in a DQL statement. For example, the following will cause the aforementioned error:

$file = Doctrine_Query::create()
     ->from('TableOfFiles f')
     ->where('f.file_name = "myfile.fil"')
     ->fetchOne();

In that case Doctrine interprets myfile to be some kind of class identifier. In addition to the PHP Fatal error, you might also see an error like this:

Couldn't find class "myfile#0 /var/webLib/symfony-1.4.11/lib/
plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Table.php

The fix for this issue is simple. Just rewrite your statement to look like this:

$file = Doctrine_Query::create()
     ->from('TableOfFiles f')
     ->where('f.file_name = \'myfile.fil\'')
     ->fetchOne();

Or even better (Thanks Steve Mobs) create a prepared statement and let Doctrine do the quotes for you:

$file = Doctrine_Query::create()
     ->from('TableOfFiles f')
     ->where('f.file_name = ?', array('myfile.fil'))
     ->fetchOne();

It’s Documented

For those who are interested, there is actually a warning about using double quotes in DQL on the doctrine-project.org DQL Page.

This entry was posted in 1.4, Symfony and tagged , , , . Bookmark the permalink.

3 Responses to Symfony 1.4: Doctrine DQL Doesn’t Like Double Quotes

  1. steve mobs says:

    what about this:
    ->where(‘f.file_name = ?’, array(‘myfile.fil’))

    leave it up to doctrine to do the quotations…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>