Introduction to Doctrine Hydrators
Doctrine Hydrators allows you to convert the database record result returned from the query into a formatted object graph or an array. The results can be easily passed to your view files directly when you convert into an array or objects. Each hydrating method has different format for returning values and advantages and disadvantages over others. Next section I’ll explain the following hydration methods one by one.
1. HYDRATE_RECORD
2. HYDRATE_ARRAY
3. HYDRATE_SCALER
Doctrine Query Result Hydration Examples
HYDRATE_RECORD
The resultset returned from the query will be converted to an object graph which can be accessed using object oriented style. Once you use this method it’s easy to manipulate the resultset since we know the exact properties of object. But this method takes time to load and normally considered slow.
Sample Output
Doctrine_Collection Object ( [data:protected] => Array ( [0] => User Object ( [_node:protected] => [_id:protected] => Array ( [userID] => 10 ) [_data:protected] => Array ( [userID] => 10 [username] => adam )
Using inside Doctrine Models
$get_user = Doctrine_Query::create() ->select('u.*') ->from('User u') ->where('username = ?', 'adam'); $get_user_result = $get_user->execute(array(), Doctrine_Core::HYDRATE_RECORD); $username = $get_user_result[0]->username;
HYDRATE_ARRAY
The resultset returned from the query will be converted to an array which can be accessed using array key=>value pairs. Arrays are little bit harder to work with compared to objects since we need to remember the array keys to get values. But this method is faster than object graphs.
Sample Output
Array ( [0] => Array ( [userID] => 10 [username] => adam ) )
Using inside Doctrine Models
$get_user = Doctrine_Query::create() ->select('u.*') ->from('User u') ->where('username = ?', 'adam'); $get_user_result = $get_user->execute(array(), Doctrine_Core::HYDRATE_ARRAY); $username = $get_user_result[0]['username'];
HYDRATE_SCALER
The resultset returned from the query will be converted to a flat array with all the records included in relationship tables as well. This results are very hard to work with since all the relationship tables data will be repeated and hard to maintain. But this method is fastest method to access results sets using doctrine.
Sample Output
Array ( [0] => Array ( [u_userID] => 10 [u_username] => adam ) )
Using inside Doctrine Models
$get_user = Doctrine_Query::create() ->select('u.*') ->from('User u') ->where('username = ?', 'adam'); $get_user_result = $get_user->execute(array(), Doctrine_Core::HYDRATE_ARRAY); $username = $get_user_result[0]['u_username'];
Choosing which hydrate method to use depends on your development preferences and performance targets.
Converting Resultset to Array without using Hydrators
$get_user = Doctrine_Query::create() ->select('u.*') ->from('User u') ->where('username = ?', 'adam'); $get_user_result = $get_user->execute()->toArray; $username = $get_user_result[0]['username'];
Leave a Reply