. */ /** * Hydration strategy used for creating graphs of entities. * * @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 * @todo Rename to ObjectDriver */ 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()->getAssociationMapping($name); $relatedClass = $this->_em->getClassMetadata($relation->getTargetEntityName()); $coll = $this->getElementCollection($relatedClass->getClassName()); $coll->setReference($entity, $relation); $entity->_internalSetReference($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->_internalGetReference($property)->add($entity2, $entity2->_internalGetField($indexField)); } public function addRelatedElement(Doctrine_Entity $entity1, $property, Doctrine_Entity $entity2) { $entity1->_internalGetReference($property)->add($entity2); } public function setRelatedElement(Doctrine_Entity $entity1, $property, $entity2) { $entity1->_internalSetReference($property, $entity2); } public function isIndexKeyInUse(Doctrine_Entity $entity, $assocField, $indexField) { return $entity->_internalGetReference($assocField)->contains($indexField); } public function isFieldSet(Doctrine_Entity $entity, $field) { return $entity->contains($field); } public function getFieldValue(Doctrine_Entity $entity, $field) { return $entity->_internalGetField($field); } public function getReferenceValue(Doctrine_Entity $entity, $field) { return $entity->_internalGetReference($field); } public function addElementToIndexedCollection($coll, $entity, $keyField) { $coll->add($entity, $entity->_internalGetField($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(); } }