. */ /** * Doctrine_Hydrator_RecordDriver * Hydration strategy used for creating graphs of entity objects. * * @package Doctrine * @subpackage Hydrate * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.phpdoctrine.org * @since 1.0 * @version $Revision$ * @author Konsta Vesterinen * @author Roman Borschel */ class Doctrine_Hydrator_RecordDriver { /** Collections initialized by the driver */ protected $_collections = array(); /** Memory for initialized relations */ private $_initializedRelations = array(); /** Null object */ private $_nullObject; /** The EntityManager */ private $_em; public function __construct(Doctrine_EntityManager $em) { $this->_nullObject = Doctrine_Null::$INSTANCE; $this->_em = $em; } public function getElementCollection($component) { $coll = new Doctrine_Collection($component); $this->_collections[] = $coll; return $coll; } public function getLastKey($coll) { // check needed because of mixed results if (is_array($coll)) { end($coll); return key($coll); } else { $coll->end(); return $coll->key(); } } public function initRelatedCollection(Doctrine_Entity $entity, $name) { if ( ! isset($this->_initializedRelations[$entity->getOid()][$name])) { $relation = $entity->getClass()->getRelation($name); $relatedClass = $relation->getTable(); $coll = $this->getElementCollection($relatedClass->getClassName()); $coll->setReference($entity, $relation); $entity->_rawSetReference($name, $coll); $this->_initializedRelations[$entity->getOid()][$name] = true; } } public function registerCollection(Doctrine_Collection $coll) { $this->_collections[] = $coll; } public function getNullPointer() { return $this->_nullObject; } public function getElement(array $data, $className) { return $this->_em->createEntity($className, $data); } public function addRelatedIndexedElement(Doctrine_Entity $entity1, $property, Doctrine_Entity $entity2, $indexField) { $entity1->_rawGetReference($property)->add($entity2, $entity2->_rawGetField($indexField)); } public function addRelatedElement(Doctrine_Entity $entity1, $property, Doctrine_Entity $entity2) { $entity1->_rawGetReference($property)->add($entity2); } public function setRelatedElement(Doctrine_Entity $entity1, $property, $entity2) { $entity1->_rawSetReference($property, $entity2); } public function isIndexKeyInUse(Doctrine_Entity $entity, $assocField, $indexField) { return $entity->_rawGetReference($assocField)->contains($indexField); } public function isFieldSet(Doctrine_Entity $entity, $field) { return $entity->contains($field); } public function getFieldValue(Doctrine_Entity $entity, $field) { return $entity->_rawGetField($field); } public function getReferenceValue(Doctrine_Entity $entity, $field) { return $entity->_rawGetReference($field); } public function addElementToIndexedCollection($coll, $entity, $keyField) { $coll->add($entity, $entity->_rawGetField($keyField)); } public function addElementToCollection($coll, $entity) { $coll->add($entity); } public function flush() { // take snapshots from all initialized collections foreach ($this->_collections as $coll) { $coll->takeSnapshot(); } $this->_collections = array(); $this->_initializedRelations = array(); } }