Php – Doctrine – Can I use real SQL (not DQL) in a ->andWhere()

doctrinedoctrine-queryormPHP

I'm using Doctrine, a PHP ORM. I have created a Doctrine Query, and I need more control. So I've started to use the ->andWhere(...) methods to add new where clauses. However I need to do a subquery based on another table, like so:

$query->andWhere("id in (SELECT id from other_table where value = ?)", $myvar);

The above doesn't work. However there is no Doctrine class for other_table, and doctrine keeps trying to load the file other_file.php. I have figured out that it is interpreting this as DQL (right?). Is there someway I can tell Doctrine to not interpret this as DQL, so that I can just use raw SQL?

I know I could use Doctrine_RawSql, but that would involve rewriting all of this query. It would be nice if there was a halfway house.

Best Answer

Unfortunately with Doctrine 1.x it is not possible to do this, even using RawSql.

All RawSql queries require that every table selected be mapped to a 'Component', which is really a Doctrine_Record class.

You might be better served adding a class for that other table, even if it's stupidly simple and never used anywhere else.

Otherwise, you'll have to drop to raw PDO using:

$dbh = Doctrine_Manager::connection()->getDbh();

See Doctrine_RawSql