What is hydration teaching?

I read about hydration in the documentation of the doctrine, but I still don't understand what it is.

Can someone explain?

+87
php doctrine hydration
Apr 18 '10 at 9:35 a.m.
source share
3 answers

Hydration is a method used to return query results. For example:

  • HYDRATE_ARRAY - This will return you an array of records that are represented by another array:

     $q = Doctrine_Query::create() ->from('Post p') ->setHydrationMode(Doctrine::HYDRATE_ARRAY); $resultSet = $q->execute(); // $resultSet is an array foreach ($resultSet as $post) { // $post is an array echo $post['title']; } 
  • HYDRATE_RECORD - This will return you a collection ( Doctrine_Collection ) of objects:

     $q = Doctrine_Query::create() ->from('Post p') ->setHydrationMode(Doctrine::HYDRATE_RECORD); // Unnecessary, HYDATE_RECORD is default method $resultSet = $q->execute(); // $resultSet is an Doctrine_Collection object foreach ($resultSet as $post) { // $post is an Post object echo $post->getTitle(); echo $post['title']; // Each Doctrine Model object implements ArrayAccess interface so this is possible echo $post->myCustomMethod(); } 
  • HYDRATE_SINGULAR_SCALAR - will return the value of the first column of the query result:

      $q = Doctrine_Query::create() ->select('p.created_at') ->from('Post p') ->where('p.id = ?', 321) ->setHydrationMode(Doctrine::HYDRATE_SINGULAR_SCALAR); $createdAt = $q->execute(); // $createdAt has value of first column from first record from result set (eg.: 2008-04-06 21:22:35) 

There are several more methods that you can read about in the documentation.

+90
Apr 18 2018-10-18T00:
source share
+9
Sep 05 2018-11-11T00:
source share
 $q->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY); 

It will return a simple array instead of a doctrine collection object.

+3
Dec 04 '11 at 18:29
source share



All Articles