2007-11-20 17:26:42 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-11-20 17:26:42 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2008-05-06 17:41:22 +04:00
|
|
|
* Doctrine_Hydrator_RecordDriver
|
2008-04-13 00:11:11 +04:00
|
|
|
* Hydration strategy used for creating graphs of entity objects.
|
2007-11-20 17:26:42 +03:00
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @subpackage Hydrate
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2007-11-20 17:26:42 +03:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
2008-01-12 22:49:11 +03:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2007-11-20 17:26:42 +03:00
|
|
|
*/
|
2008-03-19 21:33:14 +03:00
|
|
|
class Doctrine_Hydrator_RecordDriver
|
2007-11-20 17:26:42 +03:00
|
|
|
{
|
2008-05-06 17:41:22 +04:00
|
|
|
/** Collections initialized by the driver */
|
2007-11-20 17:26:42 +03:00
|
|
|
protected $_collections = array();
|
2008-05-06 17:41:22 +04:00
|
|
|
/** Memory for initialized relations */
|
|
|
|
private $_initializedRelations = array();
|
|
|
|
/** Null object */
|
2008-03-19 21:33:14 +03:00
|
|
|
private $_nullObject;
|
2008-05-14 01:20:34 +04:00
|
|
|
/** The EntityManager */
|
|
|
|
private $_em;
|
2008-03-19 21:33:14 +03:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
public function __construct(Doctrine_EntityManager $em)
|
2008-03-19 21:33:14 +03:00
|
|
|
{
|
|
|
|
$this->_nullObject = Doctrine_Null::$INSTANCE;
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->_em = $em;
|
2008-03-19 21:33:14 +03:00
|
|
|
}
|
2007-11-20 17:26:42 +03:00
|
|
|
|
|
|
|
public function getElementCollection($component)
|
|
|
|
{
|
|
|
|
$coll = new Doctrine_Collection($component);
|
|
|
|
$this->_collections[] = $coll;
|
|
|
|
|
|
|
|
return $coll;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLastKey($coll)
|
|
|
|
{
|
2008-05-06 17:41:22 +04:00
|
|
|
// check needed because of mixed results
|
|
|
|
if (is_array($coll)) {
|
|
|
|
end($coll);
|
|
|
|
return key($coll);
|
|
|
|
} else {
|
|
|
|
$coll->end();
|
|
|
|
return $coll->key();
|
|
|
|
}
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
public function initRelatedCollection(Doctrine_Entity $entity, $name)
|
2007-11-20 17:26:42 +03:00
|
|
|
{
|
2008-05-14 01:20:34 +04:00
|
|
|
if ( ! isset($this->_initializedRelations[$entity->getOid()][$name])) {
|
2008-07-21 00:13:24 +04:00
|
|
|
$relation = $entity->getClass()->getRelation($name);
|
2008-05-06 17:41:22 +04:00
|
|
|
$relatedClass = $relation->getTable();
|
|
|
|
$coll = $this->getElementCollection($relatedClass->getClassName());
|
2008-05-14 01:20:34 +04:00
|
|
|
$coll->setReference($entity, $relation);
|
2008-06-15 19:56:28 +04:00
|
|
|
$entity->_rawSetReference($name, $coll);
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->_initializedRelations[$entity->getOid()][$name] = true;
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerCollection(Doctrine_Collection $coll)
|
|
|
|
{
|
|
|
|
$this->_collections[] = $coll;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNullPointer()
|
|
|
|
{
|
2008-03-19 21:33:14 +03:00
|
|
|
return $this->_nullObject;
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|
|
|
|
|
2008-03-05 14:24:33 +03:00
|
|
|
public function getElement(array $data, $className)
|
2007-11-20 17:26:42 +03:00
|
|
|
{
|
2008-05-17 16:22:24 +04:00
|
|
|
return $this->_em->createEntity($className, $data);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addRelatedIndexedElement(Doctrine_Entity $entity1, $property,
|
|
|
|
Doctrine_Entity $entity2, $indexField)
|
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
$entity1->_rawGetReference($property)->add($entity2, $entity2->_rawGetField($indexField));
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addRelatedElement(Doctrine_Entity $entity1, $property,
|
|
|
|
Doctrine_Entity $entity2)
|
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
$entity1->_rawGetReference($property)->add($entity2);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setRelatedElement(Doctrine_Entity $entity1, $property, $entity2)
|
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
$entity1->_rawSetReference($property, $entity2);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isIndexKeyInUse(Doctrine_Entity $entity, $assocField, $indexField)
|
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
return $entity->_rawGetReference($assocField)->contains($indexField);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isFieldSet(Doctrine_Entity $entity, $field)
|
|
|
|
{
|
|
|
|
return $entity->contains($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFieldValue(Doctrine_Entity $entity, $field)
|
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
return $entity->_rawGetField($field);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getReferenceValue(Doctrine_Entity $entity, $field)
|
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
return $entity->_rawGetReference($field);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addElementToIndexedCollection($coll, $entity, $keyField)
|
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
$coll->add($entity, $entity->_rawGetField($keyField));
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addElementToCollection($coll, $entity)
|
|
|
|
{
|
|
|
|
$coll->add($entity);
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function flush()
|
|
|
|
{
|
|
|
|
// take snapshots from all initialized collections
|
2008-04-13 00:11:11 +04:00
|
|
|
foreach ($this->_collections as $coll) {
|
2007-11-20 17:26:42 +03:00
|
|
|
$coll->takeSnapshot();
|
|
|
|
}
|
2008-03-05 14:24:33 +03:00
|
|
|
$this->_collections = array();
|
2008-05-06 17:41:22 +04:00
|
|
|
$this->_initializedRelations = array();
|
2008-01-12 22:49:11 +03:00
|
|
|
}
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|