I can't find a way to set the default hydrator in Doctrine. It should be available. Right?
The above documentation page explains how to create a custom hydrator. The drawback here is that you need to "specify" the hydrator each and every time you execute a query.
That'd be a great idea, and on reading your question I thought it'd be something you could do via Doctrine. However, reading through the code makes me think you can't:
Doctrine_Query::create()
creates a new query specifying only the first argument ofDoctrine_Query_Abstract::__construct()
, the connection, without specifying the second argument - the hydration mode. No calls to configuration are made. As no hydrator is passed, a newDoctrine_Hydrator
is created, and its constructor equally does not look anywhere for a configuration option, and thus it has the defaultDoctrine::HYDRATE_RECORD
setting.Perhaps subclassing Doctrine_Query with the below factory method is the easiest option?
I figured this out by reading Chris Gutierrez's comment and changing some stuff.
First, define an extension class for Doctrine_Query. Extend the constructor to define your own hydration mode.
Then, in your bootstrap, tell Doctrine about your new class.
Chris Gutierrez defined the attribute for the connection instead of globally but I have more than one connection and I want to use this default for all of them.
Now you don't have to call Doctrine_Query::setHydrationMode() every time you build a query.
Here's more information
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/configuration/en#configure-query-class
EDIT: Changes below
I have found a problem with the above. Specifically, doing something like "Doctrine_Core::getTable('Model')->find(1)" will always return a hydrated array, not an object. So I have altered this a bit, defining custom execute methods for use in a Query call.
Also, I added memory freeing code.