21Dec/100
[Watch out] Doctrine_Query::fetchOne()
Problem - fetchOne does not fetch one record from the database
Doctrine_Query has method which returns one row, it's called fetchOne.
This method is quite useful because sometimes we just don't need all records that a query can return.
But there's a "small" problem with that method. It does not fetch one record!
Well maybe it's not quite true because it returns one row, but at the back-end it fetches the whole collection.
Almost everyone knows that it's the worst solution to fetch all data into PHP from the database to make use just of one of the records. Sadly that is how the Doctrine_Query::fetchOne() really works.
Solution
Simple solution for that is to specify limit by hand, before executing fetchOne().
/* @var $query Doctrine_Query */ $query->limit(1); $record = $query->fetchOne();