Commit ab65ad5b authored by romanb's avatar romanb

Refactorings. Started with new hydrator for 2.0.

parent ff112209
...@@ -52,7 +52,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab ...@@ -52,7 +52,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
* *
* @var string * @var string
*/ */
protected $_customMapperClassName; protected $_customRepositoryClassName;
/** /**
* *
...@@ -132,7 +132,6 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab ...@@ -132,7 +132,6 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
* additional keys: * additional keys:
* -- notnull whether or not the column is marked as notnull * -- notnull whether or not the column is marked as notnull
* -- values enum values * -- values enum values
* -- notblank notblank validator + notnull constraint
* ... many more * ... many more
* *
* @var array $columns * @var array $columns
...@@ -322,7 +321,10 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab ...@@ -322,7 +321,10 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
*/ */
public function isIdentifier($fieldName) public function isIdentifier($fieldName)
{ {
return in_array($fieldName, $this->getIdentifier()); if ($this->_identifierType != Doctrine::IDENTIFIER_COMPOSITE) {
return $fieldName === $this->_identifier[0];
}
return in_array($fieldName, $this->_identifier);
} }
/** /**
...@@ -524,6 +526,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab ...@@ -524,6 +526,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
*/ */
public function mapColumn($name, $type, $length = null, $options = array(), $prepend = false) public function mapColumn($name, $type, $length = null, $options = array(), $prepend = false)
{ {
// converts 0 => 'primary' to 'primary' => true etc.
foreach ($options as $k => $option) { foreach ($options as $k => $option) {
if (is_numeric($k)) { if (is_numeric($k)) {
if ( ! empty($option) && $option !== false) { if ( ! empty($option) && $option !== false) {
...@@ -554,7 +557,42 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab ...@@ -554,7 +557,42 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
$this->_fieldNames[$name] = $fieldName; $this->_fieldNames[$name] = $fieldName;
} }
// Inspect & fill $options
if ($length == null) { if ($length == null) {
$length = $this->_getDefaultLength($type);
}
$options['type'] = $type;
$options['length'] = $length;
if ( ! $this->_hasDefaultValues && isset($options['default'])) {
$this->_hasDefaultValues = true;
}
if ( ! empty($options['primary'])) {
if ( ! in_array($fieldName, $this->_identifier)) {
$this->_identifier[] = $fieldName;
}
/*if (isset($options['autoincrement']) && $options['autoincrement'] === true) {
}*/
}
/*
if ( ! isset($options['immutable'])) {
$options['immutable'] = false;
}*/
if ($prepend) {
$this->_mappedColumns = array_merge(array($name => $options), $this->_mappedColumns);
} else {
$this->_mappedColumns[$name] = $options;
}
$this->_columnCount++;
}
private function _getDefaultLength($type)
{
switch ($type) { switch ($type) {
case 'string': case 'string':
case 'clob': case 'clob':
...@@ -565,44 +603,21 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab ...@@ -565,44 +603,21 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
case 'blob': case 'blob':
case 'gzip': case 'gzip':
// use php int max // use php int max
$length = 2147483647; return 2147483647;
break;
case 'boolean': case 'boolean':
$length = 1; return 1;
case 'date': case 'date':
// YYYY-MM-DD ISO 8601 // YYYY-MM-DD ISO 8601
$length = 10; return 10;
case 'time': case 'time':
// HH:NN:SS+00:00 ISO 8601 // HH:NN:SS+00:00 ISO 8601
$length = 14; return 14;
case 'timestamp': case 'timestamp':
// YYYY-MM-DDTHH:MM:SS+00:00 ISO 8601 // YYYY-MM-DDTHH:MM:SS+00:00 ISO 8601
$length = 25; return 25;
break;
} }
} }
$options['type'] = $type;
$options['length'] = $length;
if ($prepend) {
$this->_mappedColumns = array_merge(array($name => $options), $this->_mappedColumns);
} else {
$this->_mappedColumns[$name] = $options;
}
if ( ! empty($options['primary'])) {
if ( ! in_array($fieldName, $this->_identifier)) {
$this->_identifier[] = $fieldName;
}
}
if (isset($options['default'])) {
$this->_hasDefaultValues = true;
}
$this->_columnCount++;
}
/** /**
* setColumn * setColumn
* *
...@@ -1733,6 +1748,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab ...@@ -1733,6 +1748,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
* Registers a custom mapper for the entity class. * Registers a custom mapper for the entity class.
* *
* @param string $mapperClassName The class name of the custom mapper. * @param string $mapperClassName The class name of the custom mapper.
* @deprecated
*/ */
public function setCustomMapperClass($mapperClassName) public function setCustomMapperClass($mapperClassName)
{ {
...@@ -1740,7 +1756,22 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab ...@@ -1740,7 +1756,22 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
throw new Doctrine_ClassMetadata_Exception("The custom mapper must be a subclass" throw new Doctrine_ClassMetadata_Exception("The custom mapper must be a subclass"
. " of Doctrine_Mapper."); . " of Doctrine_Mapper.");
} }
$this->_customMapperClassName = $mapperClassName; $this->_customRepositoryClassName = $mapperClassName;
}
/**
* Registers a custom mapper for the entity class.
*
* @param string $mapperClassName The class name of the custom mapper.
* @deprecated
*/
public function setCustomRepositoryClass($repositoryClassName)
{
if ( ! is_subclass_of($repositoryClassName, 'Doctrine_EntityRepository')) {
throw new Doctrine_ClassMetadata_Exception("The custom repository must be a subclass"
. " of Doctrine_EntityRepository.");
}
$this->_customRepositoryClassName = $repositoryClassName;
} }
/** /**
...@@ -1748,16 +1779,22 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab ...@@ -1748,16 +1779,22 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
* *
* @return string|null The name of the custom mapper class or NULL if the entity * @return string|null The name of the custom mapper class or NULL if the entity
* class does not have a custom mapper class. * class does not have a custom mapper class.
* @deprecated
*/ */
public function getCustomMapperClass() public function getCustomMapperClass()
{ {
return $this->_customMapperClassName; return $this->_customRepositoryClassName;
}
public function getCustomRepositoryClass()
{
return $this->_customRepositoryClassName;
} }
/** /**
* @todo Thoughts & Implementation. * @todo Thoughts & Implementation.
*/ */
public function setType($type) public function setEntityType($type)
{ {
//Doctrine::CLASSTYPE_ENTITY //Doctrine::CLASSTYPE_ENTITY
//Doctrine::CLASSTYPE_MAPPED_SUPERCLASS //Doctrine::CLASSTYPE_MAPPED_SUPERCLASS
......
...@@ -449,7 +449,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -449,7 +449,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
} }
} else { } else {
// @todo does not take composite keys into account // @todo does not take composite keys into account
$list[] = $record->getIncremented(); $ids = $record->identifier();
$list[] = count($ids) > 0 ? array_pop($ids) : null;
} }
} }
...@@ -564,7 +565,9 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -564,7 +565,9 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
if ( ! isset($name)) { if ( ! isset($name)) {
foreach ($this->data as $record) { foreach ($this->data as $record) {
$value = $record->getIncremented(); // FIXME: composite key support
$ids = $record->identifier();
$value = count($ids) > 0 ? array_pop($ids) : null;
if ($value !== null) { if ($value !== null) {
$list[] = $value; $list[] = $value;
} }
...@@ -583,7 +586,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -583,7 +586,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
} }
} else { } else {
foreach ($this->data as $record) { foreach ($this->data as $record) {
$value = $record->getIncremented(); $ids = $record->identifier();
$value = count($ids) > 0 ? array_pop($ids) : null;
if ($value !== null) { if ($value !== null) {
$list[] = $value; $list[] = $value;
} }
......
This diff is collapsed.
...@@ -20,7 +20,23 @@ ...@@ -20,7 +20,23 @@
*/ */
/** /**
* Doctrine_Connection_UnitOfWork * The UnitOfWork is responsible for writing out changes to the database at
* the correct time and in the correct order.
*
* Some terminology:
*
* <b>New entity</b>: From the point of view of the unitOfWork is an entity that
* already has an identity but is not yet persisted into the database. This
* is usually the case for all newly saved entities that use a SEQUENCE id
* generator. Entities with an IDENTITY id generator get persisted as soon
* as they're saved in order to obtain the identifier. Therefore entities that
* use an IDENTITY id generator never appear in the list of new entities of the UoW.
*
* <b>Dirty entity</b>: ...
*
* <b>Removed entity</b>: ...
*
* <b>Clean entity</b>: ...
* *
* @package Doctrine * @package Doctrine
* @subpackage Connection * @subpackage Connection
...@@ -105,12 +121,19 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module ...@@ -105,12 +121,19 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
*/ */
public function registerNew(Doctrine_Record $entity) public function registerNew(Doctrine_Record $entity)
{ {
if (isset($this->_dirtyEntities[$entity->getOid()])) { if ( ! $entity->identifier()) {
throw new Doctrine_Connection_Exception("Entity without identity "
. "can't be registered as new.");
}
$oid = $entity->getOid();
if (isset($this->_dirtyEntities[$oid])) {
throw new Doctrine_Connection_Exception("Dirty object can't be registered as new."); throw new Doctrine_Connection_Exception("Dirty object can't be registered as new.");
} else if (isset($this->_removedEntities[$entity->getOid()])) { } else if (isset($this->_removedEntities[$oid])) {
throw new Doctrine_Connection_Exception("Removed object can't be registered as new."); throw new Doctrine_Connection_Exception("Removed object can't be registered as new.");
} else if (isset($this->_newEntities[$oid])) {
throw new Doctrine_Connection_Exception("Object already registered as new. Can't register twice.");
} }
$this->_newEntities[$entity->getOid()] = $entity; $this->_newEntities[$oid] = $entity;
} }
public function isRegisteredNew(Doctrine_Record $entity) public function isRegisteredNew(Doctrine_Record $entity)
...@@ -131,13 +154,18 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module ...@@ -131,13 +154,18 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
*/ */
public function registerDirty(Doctrine_Record $entity) public function registerDirty(Doctrine_Record $entity)
{ {
if ( ! $entity->identifier()) {
throw new Doctrine_Connection_Exception("Entity without identity "
. "can't be registered as dirty.");
}
$oid = $entity->getOid();
if (isset($this->_removedEntities[$entity->getOid()])) { if (isset($this->_removedEntities[$entity->getOid()])) {
throw new Doctrine_Connection_Exception("Removed object can't be registered as dirty."); throw new Doctrine_Connection_Exception("Removed object can't be registered as dirty.");
} else if (isset($this->_newEntities[$entity->getOid()])) {
throw new Doctrine_Connection_Exception("");
} }
if ( ! isset($this->_dirtyEntities[$oid], $this->_newEntities[$oid])) {
$this->_dirtyEntities[$entity->getOid()] = $entity; $this->_dirtyEntities[$entity->getOid()] = $entity;
} }
}
public function isRegisteredDirty(Doctrine_Record $entity) public function isRegisteredDirty(Doctrine_Record $entity)
{ {
...@@ -149,8 +177,21 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module ...@@ -149,8 +177,21 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
*/ */
public function registerRemoved(Doctrine_Record $entity) public function registerRemoved(Doctrine_Record $entity)
{ {
if ($entity->isTransient()) {
return;
}
$this->unregisterIdentity($entity); $this->unregisterIdentity($entity);
$this->_removedEntities[$entity->getOid()] = $entity; $oid = $entity->getOid();
if (isset($this->_newEntities[$oid])) {
unset($this->_newEntities[$oid]);
return;
}
if (isset($this->_dirtyEntities[$oid])) {
unset($this->_dirtyEntities[$oid]);
}
if ( ! isset($this->_removedEntities[$oid])) {
$this->_removedEntities[$oid] = $entity;
}
} }
public function isRegisteredRemoved(Doctrine_Record $entity) public function isRegisteredRemoved(Doctrine_Record $entity)
......
<?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
* <http://www.phpdoctrine.org>.
*/
/**
* Base class for all custom user-defined repositories.
* Provides basic finder methods, common to all repositories.
*
* @package Doctrine
* @subpackage EntityRepository
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.org
* @since 2.0
* @version $Revision$
* @author Roman Borschel <roman@code-factory.org>
* @todo package:orm
*/
class Doctrine_EntityRepository
{
protected $_entityName;
protected $_conn;
protected $_classMetadata;
public function __construct($entityName, Doctrine_ClassMetadata $classMetadata)
{
$this->_entityName = $entityName;
$this->_conn = $classMetadata->getConnection();
$this->_classMetadata = $classMetadata;
}
/**
* createQuery
* creates a new Doctrine_Query object and adds the component name
* of this table as the query 'from' part
*
* @param string Optional alias name for component aliasing.
*
* @return Doctrine_Query
*/
protected function _createQuery($alias = '')
{
if ( ! empty($alias)) {
$alias = ' ' . trim($alias);
}
return Doctrine_Query::create($this->_conn)->from($this->_entityName . $alias);
}
/**
* clear
* clears the first level cache (identityMap)
*
* @return void
* @todo what about a more descriptive name? clearIdentityMap?
*/
public function clear()
{
$this->_conn->unitOfWork->clearIdentitiesForEntity($this->_classMetadata->getRootClassName());
}
/**
* Finds an entity by its primary key.
*
* @param $id database row id
* @param int $hydrationMode Doctrine::HYDRATE_ARRAY or Doctrine::HYDRATE_RECORD
* @return mixed Array or Doctrine_Record or false if no result
* @todo Remove. Move to EntityRepository.
*/
public function find($id, $hydrationMode = null)
{
if (is_null($id)) {
return false;
}
if (is_array($id) && count($id) > 1) {
// it's a composite key. keys = field names, values = values.
$values = array_values($id);
$keys = array_keys($id);
} else {
$values = is_array($id) ? array_values($id) : array($id);
$keys = $this->_classMetadata->getIdentifier();
}
return $this->_createQuery()
->where(implode(' = ? AND ', $keys) . ' = ?')
->fetchOne($values, $hydrationMode);
}
/**
* Finds all entities of the mapper's class.
* Use with care.
*
* @param int $hydrationMode Doctrine::HYDRATE_ARRAY or Doctrine::HYDRATE_RECORD
* @return Doctrine_Collection
*/
public function findAll($hydrationMode = null)
{
return $this->_createQuery()->execute(array(), $hydrationMode);
}
/**
* findBy
*
* @param string $column
* @param string $value
* @param string $hydrationMode
* @return void
*/
protected function findBy($fieldName, $value, $hydrationMode = null)
{
return $this->_createQuery()->where($fieldName . ' = ?')->execute(array($value), $hydrationMode);
}
/**
* findOneBy
*
* @param string $column
* @param string $value
* @param string $hydrationMode
* @return void
*/
protected function findOneBy($fieldName, $value, $hydrationMode = null)
{
$results = $this->_createQuery()->where($fieldName . ' = ?')->limit(1)->execute(
array($value), $hydrationMode);
return $hydrationMode === Doctrine::HYDRATE_ARRAY ? array_shift($results) : $results->getFirst();
}
/**
* findBySql
* finds records with given SQL where clause
* returns a collection of records
*
* @param string $dql DQL after WHERE clause
* @param array $params query parameters
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
* @return Doctrine_Collection
*
* @todo This actually takes DQL, not SQL, but it requires column names
* instead of field names. This should be fixed to use raw SQL instead.
*/
public function findBySql($dql, array $params = array(), $hydrationMode = null)
{
return $this->_createQuery()->where($dql)->execute($params, $hydrationMode);
}
/**
* findByDql
* finds records with given DQL where clause
* returns a collection of records
*
* @param string $dql DQL after WHERE clause
* @param array $params query parameters
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
* @return Doctrine_Collection
*/
public function findByDql($dql, array $params = array(), $hydrationMode = null)
{
$query = new Doctrine_Query($this->_conn);
$component = $this->getComponentName();
$dql = 'FROM ' . $component . ' WHERE ' . $dql;
return $query->query($dql, $params, $hydrationMode);
}
/**
* Adds support for magic finders.
* findByColumnName, findByRelationAlias
* findById, findByContactId, etc.
*
* @return void
* @throws Doctrine_Mapper_Exception If the method called is an invalid find* method
* or no find* method at all and therefore an invalid
* method call.
*/
public function __call($method, $arguments)
{
if (substr($method, 0, 6) == 'findBy') {
$by = substr($method, 6, strlen($method));
$method = 'findBy';
} else if (substr($method, 0, 9) == 'findOneBy') {
$by = substr($method, 9, strlen($method));
$method = 'findOneBy';
} else {
throw new Doctrine_Mapper_Exception("Undefined method '$method'.");
}
if (isset($by)) {
if ( ! isset($arguments[0])) {
throw new Doctrine_Mapper_Exception('You must specify the value to findBy.');
}
$fieldName = Doctrine::tableize($by);
$hydrationMode = isset($arguments[1]) ? $arguments[1]:null;
if ($this->_classMetadata->hasField($fieldName)) {
return $this->$method($fieldName, $arguments[0], $hydrationMode);
} else if ($this->_classMetadata->hasRelation($by)) {
$relation = $this->_classMetadata->getRelation($by);
if ($relation['type'] === Doctrine_Relation::MANY) {
throw new Doctrine_Mapper_Exception('Cannot findBy many relationship.');
}
return $this->$method($relation['local'], $arguments[0], $hydrationMode);
} else {
throw new Doctrine_Mapper_Exception('Cannot find by: ' . $by . '. Invalid field or relationship alias.');
}
}
}
}
\ No newline at end of file
...@@ -52,7 +52,8 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -52,7 +52,8 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
* 'table' => Table object, * 'table' => Table object,
* 'parent' => Parent DQL alias (if any), * 'parent' => Parent DQL alias (if any),
* 'relation' => Relation object (if any), * 'relation' => Relation object (if any),
* 'map' => Custom index to use as the key in the result (if any) * 'map' => Custom index to use as the key in the result (if any),
* 'agg' => List of aggregate values (sql alias => dql alias)
* ) * )
* ) * )
* @return mixed The created object/array graph. * @return mixed The created object/array graph.
...@@ -82,7 +83,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -82,7 +83,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
// Used variables during hydration // Used variables during hydration
reset($this->_queryComponents); reset($this->_queryComponents);
$rootAlias = key($this->_queryComponents); $rootAlias = key($this->_queryComponents);
$rootComponentName = $this->_queryComponents[$rootAlias]['mapper']->getComponentName(); $rootComponentName = $this->_queryComponents[$rootAlias]['table']->getComponentName();
// if only one component is involved we can make our lives easier // if only one component is involved we can make our lives easier
$isSimpleQuery = count($this->_queryComponents) <= 1; $isSimpleQuery = count($this->_queryComponents) <= 1;
// Holds hydration listeners that get called during hydration // Holds hydration listeners that get called during hydration
...@@ -110,7 +111,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -110,7 +111,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
foreach ($this->_queryComponents as $dqlAlias => $component) { foreach ($this->_queryComponents as $dqlAlias => $component) {
// disable lazy-loading of related elements during hydration // disable lazy-loading of related elements during hydration
$component['table']->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false); $component['table']->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
$componentName = $component['mapper']->getComponentName(); $componentName = $component['table']->getClassName();
$listeners[$componentName] = $component['table']->getRecordListener(); $listeners[$componentName] = $component['table']->getRecordListener();
$identifierMap[$dqlAlias] = array(); $identifierMap[$dqlAlias] = array();
$prev[$dqlAlias] = array(); $prev[$dqlAlias] = array();
...@@ -127,9 +128,8 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -127,9 +128,8 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
// //
// hydrate the data of the root entity from the current row // hydrate the data of the root entity from the current row
// //
$table = $this->_queryComponents[$rootAlias]['table']; $class = $this->_queryComponents[$rootAlias]['table'];
$mapper = $this->_queryComponents[$rootAlias]['mapper']; $componentName = $class->getComponentName();
$componentName = $mapper->getComponentName();
// just event stuff // just event stuff
$event->set('data', $rowData[$rootAlias]); $event->set('data', $rowData[$rootAlias]);
...@@ -175,9 +175,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -175,9 +175,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
foreach ($rowData as $dqlAlias => $data) { foreach ($rowData as $dqlAlias => $data) {
$index = false; $index = false;
$map = $this->_queryComponents[$dqlAlias]; $map = $this->_queryComponents[$dqlAlias];
$table = $map['table']; $componentName = $map['table']->getComponentName();
$mapper = $map['mapper'];
$componentName = $mapper->getComponentName();
// just event stuff // just event stuff
$event->set('data', $data); $event->set('data', $data);
...@@ -186,7 +184,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -186,7 +184,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
$parent = $map['parent']; $parent = $map['parent'];
$relation = $map['relation']; $relation = $map['relation'];
$relationAlias = $map['relation']->getAlias(); $relationAlias = $relation->getAlias();
$path = $parent . '.' . $dqlAlias; $path = $parent . '.' . $dqlAlias;
...@@ -195,16 +193,19 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -195,16 +193,19 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
} }
// check the type of the relation // check the type of the relation
if ( ! $relation->isOneToOne() && $driver->initRelated($prev[$parent], $relationAlias)) { if ( ! $relation->isOneToOne()) {
$oneToOne = false; $oneToOne = false;
// append element // append element
if (isset($nonemptyComponents[$dqlAlias])) { if (isset($nonemptyComponents[$dqlAlias])) {
$driver->initRelated($prev[$parent], $relationAlias);
if ( ! isset($identifierMap[$path][$id[$parent]][$id[$dqlAlias]])) { if ( ! isset($identifierMap[$path][$id[$parent]][$id[$dqlAlias]])) {
$element = $driver->getElement($data, $componentName); $element = $driver->getElement($data, $componentName);
// just event stuff // just event stuff
$event->set('data', $element); $event->set('data', $element);
$listeners[$componentName]->postHydrate($event); $listeners[$componentName]->postHydrate($event);
//-- //--
if ($field = $this->_getCustomIndexField($dqlAlias)) { if ($field = $this->_getCustomIndexField($dqlAlias)) {
// TODO: we should check this earlier. Fields used in INDEXBY // TODO: we should check this earlier. Fields used in INDEXBY
// must be unique. Then this can be removed here. // must be unique. Then this can be removed here.
...@@ -223,7 +224,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -223,7 +224,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
$index = $identifierMap[$path][$id[$parent]][$id[$dqlAlias]]; $index = $identifierMap[$path][$id[$parent]][$id[$dqlAlias]];
} }
// register collection for later snapshots // register collection for later snapshots
$driver->registerCollection($prev[$parent][$relationAlias]); //$driver->registerCollection($prev[$parent][$relationAlias]);
} }
} else { } else {
// 1-1 relation // 1-1 relation
...@@ -319,7 +320,13 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -319,7 +320,13 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
$cache[$key]['dqlAlias'] = $this->_tableAliases[strtolower(implode('__', $e))]; $cache[$key]['dqlAlias'] = $this->_tableAliases[strtolower(implode('__', $e))];
$mapper = $this->_queryComponents[$cache[$key]['dqlAlias']]['mapper']; $mapper = $this->_queryComponents[$cache[$key]['dqlAlias']]['mapper'];
$classMetadata = $mapper->getClassMetadata(); $classMetadata = $mapper->getClassMetadata();
// check whether it's an aggregate value or a regular field
if (isset($this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$columnName])) {
$fieldName = $this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$columnName];
} else {
$fieldName = $mapper->getFieldName($columnName); $fieldName = $mapper->getFieldName($columnName);
}
$cache[$key]['fieldName'] = $fieldName; $cache[$key]['fieldName'] = $fieldName;
// cache identifier information // cache identifier information
...@@ -343,10 +350,6 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -343,10 +350,6 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
$dqlAlias = $cache[$key]['dqlAlias']; $dqlAlias = $cache[$key]['dqlAlias'];
$fieldName = $cache[$key]['fieldName']; $fieldName = $cache[$key]['fieldName'];
if (isset($this->_queryComponents[$dqlAlias]['agg'][$fieldName])) {
$fieldName = $this->_queryComponents[$dqlAlias]['agg'][$fieldName];
}
if ($cache[$key]['isIdentifier']) { if ($cache[$key]['isIdentifier']) {
$id[$dqlAlias] .= '|' . $value; $id[$dqlAlias] .= '|' . $value;
} }
......
...@@ -71,8 +71,6 @@ class Doctrine_Hydrator_ArrayDriver ...@@ -71,8 +71,6 @@ class Doctrine_Hydrator_ArrayDriver
if ( ! isset($data[$name])) { if ( ! isset($data[$name])) {
$data[$name] = array(); $data[$name] = array();
} }
return true;
} }
/** /**
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
*/ */
/** /**
* Doctrine_Hydrate_RecordDriver * Doctrine_Hydrator_RecordDriver
* Hydration strategy used for creating graphs of entity objects. * Hydration strategy used for creating graphs of entity objects.
* *
* @package Doctrine * @package Doctrine
...@@ -34,8 +34,13 @@ ...@@ -34,8 +34,13 @@
*/ */
class Doctrine_Hydrator_RecordDriver class Doctrine_Hydrator_RecordDriver
{ {
/** Collections initialized by the driver */
protected $_collections = array(); protected $_collections = array();
/** Mappers */
protected $_mappers = array(); protected $_mappers = array();
/** Memory for initialized relations */
private $_initializedRelations = array();
/** Null object */
private $_nullObject; private $_nullObject;
public function __construct() public function __construct()
...@@ -53,21 +58,26 @@ class Doctrine_Hydrator_RecordDriver ...@@ -53,21 +58,26 @@ class Doctrine_Hydrator_RecordDriver
public function getLastKey($coll) public function getLastKey($coll)
{ {
// check needed because of mixed results
if (is_array($coll)) {
end($coll);
return key($coll);
} else {
$coll->end(); $coll->end();
return $coll->key(); return $coll->key();
} }
}
public function initRelated($record, $name) public function initRelated(Doctrine_Record $record, $name)
{ {
return true; if ( ! isset($this->_initializedRelations[$record->getOid()][$name])) {
/* $relation = $record->getClassMetadata()->getRelation($name);
if ( ! is_array($record)) { $relatedClass = $relation->getTable();
$record[$name]; $coll = $this->getElementCollection($relatedClass->getClassName());
return true; $coll->setReference($record, $relation);
$record[$name] = $coll;
$this->_initializedRelations[$record->getOid()][$name] = true;
} }
return false;
*/
} }
public function registerCollection(Doctrine_Collection $coll) public function registerCollection(Doctrine_Collection $coll)
...@@ -75,33 +85,6 @@ class Doctrine_Hydrator_RecordDriver ...@@ -75,33 +85,6 @@ class Doctrine_Hydrator_RecordDriver
$this->_collections[] = $coll; $this->_collections[] = $coll;
} }
/**
* isIdentifiable
* returns whether or not a given data row is identifiable (it contains
* all primary key fields specified in the second argument)
*
* @param array $row
* @param Doctrine_Table $table
* @return boolean
*/
/*public function isIdentifiable(array $row, Doctrine_Table $table)
{
$primaryKeys = $table->getIdentifierColumnNames();
if (is_array($primaryKeys)) {
foreach ($primaryKeys as $id) {
if ( ! isset($row[$id])) {
return false;
}
}
} else {
if ( ! isset($row[$primaryKeys])) {
return false;
}
}
return true;
}*/
public function getNullPointer() public function getNullPointer()
{ {
return $this->_nullObject; return $this->_nullObject;
...@@ -127,6 +110,7 @@ class Doctrine_Hydrator_RecordDriver ...@@ -127,6 +110,7 @@ class Doctrine_Hydrator_RecordDriver
} }
$this->_collections = array(); $this->_collections = array();
$this->_mappers = array(); $this->_mappers = array();
$this->_initializedRelations = array();
} }
/** /**
......
This diff is collapsed.
...@@ -90,23 +90,6 @@ class Doctrine_Mapper ...@@ -90,23 +90,6 @@ class Doctrine_Mapper
} }
} }
/**
* createQuery
* creates a new Doctrine_Query object and adds the component name
* of this table as the query 'from' part
*
* @param string Optional alias name for component aliasing.
*
* @return Doctrine_Query
*/
public function createQuery($alias = '')
{
if ( ! empty($alias)) {
$alias = ' ' . trim($alias);
}
return Doctrine_Query::create($this->_conn)->from($this->getComponentName() . $alias);
}
/** /**
* sets the connection for this class * sets the connection for this class
* *
...@@ -139,7 +122,7 @@ class Doctrine_Mapper ...@@ -139,7 +122,7 @@ class Doctrine_Mapper
*/ */
public function create(array $array = array()) public function create(array $array = array())
{ {
$record = new $this->_domainClassName($this, true); $record = new $this->_domainClassName();
$record->fromArray($array); $record->fromArray($array);
return $record; return $record;
...@@ -178,79 +161,6 @@ class Doctrine_Mapper ...@@ -178,79 +161,6 @@ class Doctrine_Mapper
return $this->_conn->unitOfWork->detach($entity); return $this->_conn->unitOfWork->detach($entity);
} }
/**
* Finds an entity by its primary key.
*
* @param $id database row id
* @param int $hydrationMode Doctrine::HYDRATE_ARRAY or Doctrine::HYDRATE_RECORD
* @return mixed Array or Doctrine_Record or false if no result
* @todo Remove. Move to EntityRepository.
*/
public function find($id, $hydrationMode = null)
{
if (is_null($id)) {
return false;
}
$id = is_array($id) ? array_values($id) : array($id);
return $this->createQuery()
->where(implode(' = ? AND ', (array) $this->_classMetadata->getIdentifier()) . ' = ?')
->fetchOne($id, $hydrationMode);
}
/**
* Finds all entities of the mapper's class.
* Use with care.
*
* @param int $hydrationMode Doctrine::HYDRATE_ARRAY or Doctrine::HYDRATE_RECORD
* @return Doctrine_Collection
* @todo Remove. Move to EntityRepository.
*/
public function findAll($hydrationMode = null)
{
return $this->createQuery()->execute(array(), $hydrationMode);
}
/**
* findBySql
* finds records with given SQL where clause
* returns a collection of records
*
* @param string $dql DQL after WHERE clause
* @param array $params query parameters
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
* @return Doctrine_Collection
*
* @todo This actually takes DQL, not SQL, but it requires column names
* instead of field names. This should be fixed to use raw SQL instead.
* @todo Remove. Move to EntityRepository.
*/
public function findBySql($dql, array $params = array(), $hydrationMode = null)
{
return $this->createQuery()->where($dql)->execute($params, $hydrationMode);
}
/**
* findByDql
* finds records with given DQL where clause
* returns a collection of records
*
* @param string $dql DQL after WHERE clause
* @param array $params query parameters
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
* @return Doctrine_Collection
* @todo Remove. Move to EntityRepository.
*/
public function findByDql($dql, array $params = array(), $hydrationMode = null)
{
$query = new Doctrine_Query($this->_conn);
$component = $this->getComponentName();
$dql = 'FROM ' . $component . ' WHERE ' . $dql;
return $query->query($dql, $params, $hydrationMode);
}
/** /**
* Executes a named query. * Executes a named query.
* *
...@@ -349,9 +259,7 @@ class Doctrine_Mapper ...@@ -349,9 +259,7 @@ class Doctrine_Mapper
} }
if ($found) { if ($found) {
$record = new $this->_domainClassName($this, true, $data); return new $this->_domainClassName(true, $data);
$data = array();
return $record;
} }
$idHash = $this->_conn->unitOfWork->getIdentifierHash($id); $idHash = $this->_conn->unitOfWork->getIdentifierHash($id);
...@@ -360,12 +268,12 @@ class Doctrine_Mapper ...@@ -360,12 +268,12 @@ class Doctrine_Mapper
$this->_classMetadata->getRootClassName())) { $this->_classMetadata->getRootClassName())) {
$record->hydrate($data); $record->hydrate($data);
} else { } else {
$record = new $this->_domainClassName($this, false, $data); $record = new $this->_domainClassName(false, $data);
$this->_conn->unitOfWork->registerIdentity($record); $this->_conn->unitOfWork->registerIdentity($record);
} }
$data = array(); $data = array();
} else { } else {
$record = new $this->_domainClassName($this, true, $data); $record = new $this->_domainClassName(true, $data);
} }
return $record; return $record;
...@@ -545,83 +453,6 @@ class Doctrine_Mapper ...@@ -545,83 +453,6 @@ class Doctrine_Mapper
return $this->_domainClassName; return $this->_domainClassName;
} }
/**
* findBy
*
* @param string $column
* @param string $value
* @param string $hydrationMode
* @return void
* @todo Remove. Move to EntityRepository.
*/
protected function findBy($fieldName, $value, $hydrationMode = null)
{
return $this->createQuery()->where($fieldName . ' = ?')->execute(array($value), $hydrationMode);
}
/**
* findOneBy
*
* @param string $column
* @param string $value
* @param string $hydrationMode
* @return void
* @todo Remove. Move to EntityRepository.
*/
protected function findOneBy($fieldName, $value, $hydrationMode = null)
{
$results = $this->createQuery()->where($fieldName . ' = ?')->limit(1)->execute(
array($value), $hydrationMode);
return $hydrationMode === Doctrine::HYDRATE_ARRAY ? array_shift($results) : $results->getFirst();
}
/**
* __call
*
* Adds support for magic finders.
* findByColumnName, findByRelationAlias
* findById, findByContactId, etc.
*
* @return void
* @throws Doctrine_Mapper_Exception If the method called is an invalid find* method
* or no find* method at all and therefore an invalid
* method call.
* @todo Remove. Move to EntityRepository.
*/
public function __call($method, $arguments)
{
if (substr($method, 0, 6) == 'findBy') {
$by = substr($method, 6, strlen($method));
$method = 'findBy';
} else if (substr($method, 0, 9) == 'findOneBy') {
$by = substr($method, 9, strlen($method));
$method = 'findOneBy';
} else {
throw new Doctrine_Mapper_Exception("Undefined method '$method'.");
}
if (isset($by)) {
if ( ! isset($arguments[0])) {
throw new Doctrine_Mapper_Exception('You must specify the value to findBy.');
}
$fieldName = Doctrine::tableize($by);
$hydrationMode = isset($arguments[1]) ? $arguments[1]:null;
if ($this->_classMetadata->hasField($fieldName)) {
return $this->$method($fieldName, $arguments[0], $hydrationMode);
} else if ($this->_classMetadata->hasRelation($by)) {
$relation = $this->_classMetadata->getRelation($by);
if ($relation['type'] === Doctrine_Relation::MANY) {
throw new Doctrine_Mapper_Exception('Cannot findBy many relationship.');
}
return $this->$method($relation['local'], $arguments[0], $hydrationMode);
} else {
throw new Doctrine_Mapper_Exception('Cannot find by: ' . $by . '. Invalid field or relationship alias.');
}
}
}
/** /**
* Saves an entity and all it's related entities. * Saves an entity and all it's related entities.
* *
...@@ -795,7 +626,12 @@ class Doctrine_Mapper ...@@ -795,7 +626,12 @@ class Doctrine_Mapper
$query = 'DELETE FROM ' . $assocTable->getTableName() $query = 'DELETE FROM ' . $assocTable->getTableName()
. ' WHERE ' . $rel->getForeign() . ' = ?' . ' WHERE ' . $rel->getForeign() . ' = ?'
. ' AND ' . $rel->getLocal() . ' = ?'; . ' AND ' . $rel->getLocal() . ' = ?';
$this->_conn->execute($query, array($r->getIncremented(), $record->getIncremented())); // FIXME: composite key support
$ids1 = $r->identifier();
$id1 = count($ids1) > 0 ? array_pop($ids1) : null;
$ids2 = $record->identifier();
$id2 = count($ids2) > 0 ? array_pop($ids2) : null;
$this->_conn->execute($query, array($id1, $id2));
} }
$assocMapper = $this->_conn->getMapper($assocTable->getComponentName()); $assocMapper = $this->_conn->getMapper($assocTable->getComponentName());
...@@ -804,7 +640,6 @@ class Doctrine_Mapper ...@@ -804,7 +640,6 @@ class Doctrine_Mapper
$assocRecord->set($assocTable->getFieldName($rel->getForeign()), $r); $assocRecord->set($assocTable->getFieldName($rel->getForeign()), $r);
$assocRecord->set($assocTable->getFieldName($rel->getLocal()), $record); $assocRecord->set($assocTable->getFieldName($rel->getLocal()), $record);
$assocMapper->save($assocRecord); $assocMapper->save($assocRecord);
//$this->saveSingleRecord($assocRecord);
} }
} }
} }
...@@ -890,19 +725,6 @@ class Doctrine_Mapper ...@@ -890,19 +725,6 @@ class Doctrine_Mapper
return true; return true;
} }
public function hasAttribute($key)
{
switch ($key) {
case Doctrine::ATTR_LOAD_REFERENCES:
case Doctrine::ATTR_QUERY_LIMIT:
case Doctrine::ATTR_COLL_KEY:
case Doctrine::ATTR_VALIDATE:
return true;
default:
return false;
}
}
public function executeQuery(Doctrine_Query $query) public function executeQuery(Doctrine_Query $query)
{ {
......
...@@ -111,7 +111,8 @@ class Doctrine_Mapper_JoinedStrategy extends Doctrine_Mapper_Strategy ...@@ -111,7 +111,8 @@ class Doctrine_Mapper_JoinedStrategy extends Doctrine_Mapper_Strategy
if ( ! $value->exists()) { if ( ! $value->exists()) {
$value->save(); $value->save();
} }
$record->set($field, $value->getIncremented()); $idValues = $value->identifier();
$record->set($field, $idValues[0]);
} }
} }
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>. * <http://www.phpdoctrine.org>.
*/ */
Doctrine::autoload('Doctrine_Query_Abstract');
/** /**
* Doctrine_Query * Doctrine_Query
* A Doctrine_Query object represents a DQL query. It is used to query databases for * A Doctrine_Query object represents a DQL query. It is used to query databases for
......
This diff is collapsed.
...@@ -94,17 +94,14 @@ class Doctrine_Relation_Association extends Doctrine_Relation ...@@ -94,17 +94,14 @@ class Doctrine_Relation_Association extends Doctrine_Relation
*/ */
public function fetchRelatedFor(Doctrine_Record $record) public function fetchRelatedFor(Doctrine_Record $record)
{ {
$id = $record->getIncremented(); // FIXME: composite key support
//var_dump($id); $ids = $record->identifier();
//echo "<br /><br />"; $id = count($ids) > 0 ? array_pop($ids) : null;
if (empty($id) || ! $this->_foreignMapper->getClassMetadata()->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { if (empty($id) || ! $this->_foreignMapper->getClassMetadata()->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
//echo "here" . $this->_foreignMapper->getAttribute(Doctrine::ATTR_LOAD_REFERENCES);
$coll = new Doctrine_Collection($this->getForeignComponentName()); $coll = new Doctrine_Collection($this->getForeignComponentName());
} else { } else {
$query = Doctrine_Query::create()->parseQuery($this->getRelationDql(1)); $query = Doctrine_Query::create()->parseQuery($this->getRelationDql(1));
//echo $query->getDql() . "<br />";
//echo $query->getSql() . "<br />";
//echo "<br /><br />";
$coll = Doctrine_Query::create()->query($this->getRelationDql(1), array($id)); $coll = Doctrine_Query::create()->query($this->getRelationDql(1), array($id));
} }
$coll->setReference($record, $this); $coll->setReference($record, $this);
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>. * <http://www.phpdoctrine.org>.
*/ */
Doctrine::autoload('Doctrine_Relation_Association');
/** /**
* Doctrine_Relation_Association_Self * Doctrine_Relation_Association_Self
* *
...@@ -76,7 +76,9 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association ...@@ -76,7 +76,9 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association
public function fetchRelatedFor(Doctrine_Record $record) public function fetchRelatedFor(Doctrine_Record $record)
{ {
$id = $record->getIncremented(); // FIXME: composite key support
$ids = $record->identifier();
$id = count($ids) > 0 ? array_pop($ids) : null;
$q = new Doctrine_RawSql(); $q = new Doctrine_RawSql();
......
...@@ -74,35 +74,11 @@ class Doctrine_Relation_Nest extends Doctrine_Relation_Association ...@@ -74,35 +74,11 @@ class Doctrine_Relation_Nest extends Doctrine_Relation_Association
return $dql; return $dql;
} }
/**
public function fetchRelatedFor(Doctrine_Record $record)
{
$id = $record->getIncremented();
if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
return new Doctrine_Collection($this->getTable());
} else {
$q = new Doctrine_Query();
$c = $this->getTable()->getComponentName();
$a = substr($c, 0, 1);
$c2 = $this->getAssociationTable()->getComponentName();
$a2 = substr($c2, 0, 1);
$q->from($c)
->innerJoin($c . '.' . $c2)
$sub = 'SELECT ' . $this->getForeign()
. ' FROM ' . $c2
. ' WHERE ' . $this->getLocal()
. ' = ?';
}
}
*/
public function fetchRelatedFor(Doctrine_Record $record) public function fetchRelatedFor(Doctrine_Record $record)
{ {
$id = $record->getIncremented(); // FIXME: composite key support
$ids = $record->identifier();
$id = count($ids) > 0 ? array_pop($ids) : null;
if (empty($id) || ! $this->_foreignMapper->getClassMetadata()->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { if (empty($id) || ! $this->_foreignMapper->getClassMetadata()->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
......
...@@ -147,12 +147,6 @@ class Doctrine_Relation_Parser ...@@ -147,12 +147,6 @@ class Doctrine_Relation_Parser
$this->getRelations(); $this->getRelations();
return $this->getRelation($alias, false); return $this->getRelation($alias, false);
} else { } else {
/*try {
throw new Exception();
} catch (Exception $e) {
//echo "" . "<br />";
///echo $e->getTraceAsString() . "<br /><br /><br />";
}*/
throw new Doctrine_Relation_Exception("Unknown relation '$alias'."); throw new Doctrine_Relation_Exception("Unknown relation '$alias'.");
} }
} }
......
...@@ -84,7 +84,8 @@ class Doctrine_Validator ...@@ -84,7 +84,8 @@ class Doctrine_Validator
if ($value === Doctrine_Null::$INSTANCE) { if ($value === Doctrine_Null::$INSTANCE) {
$value = null; $value = null;
} else if ($value instanceof Doctrine_Record) { } else if ($value instanceof Doctrine_Record) {
$value = $value->getIncremented(); $ids = $value->identifier();
$value = count($ids) > 0 ? array_pop($ids) : null;
} }
$dataType = $classMetadata->getTypeOf($fieldName); $dataType = $classMetadata->getTypeOf($fieldName);
......
This diff is collapsed.
...@@ -18,6 +18,8 @@ class Orm_UnitOfWorkTestCase extends Doctrine_OrmTestCase ...@@ -18,6 +18,8 @@ class Orm_UnitOfWorkTestCase extends Doctrine_OrmTestCase
public function testRegisterNew() public function testRegisterNew()
{ {
$this->_user->username = 'romanb';
$this->_user->id = 1;
$this->_unitOfWork->registerNew($this->_user); $this->_unitOfWork->registerNew($this->_user);
$this->assertFalse($this->_unitOfWork->contains($this->_user)); $this->assertFalse($this->_unitOfWork->contains($this->_user));
$this->assertTrue($this->_unitOfWork->isRegisteredNew($this->_user)); $this->assertTrue($this->_unitOfWork->isRegisteredNew($this->_user));
...@@ -35,7 +37,15 @@ class Orm_UnitOfWorkTestCase extends Doctrine_OrmTestCase ...@@ -35,7 +37,15 @@ class Orm_UnitOfWorkTestCase extends Doctrine_OrmTestCase
$this->assertTrue($this->_unitOfWork->isRegisteredDirty($this->_user)); $this->assertTrue($this->_unitOfWork->isRegisteredDirty($this->_user));
$this->assertFalse($this->_unitOfWork->isRegisteredNew($this->_user)); $this->assertFalse($this->_unitOfWork->isRegisteredNew($this->_user));
$this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user)); $this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user));
}
public function testRegisterRemovedOnTransientEntityIsIgnored()
{
$this->_user->username = 'romanb';
$this->_user->id = 1;
$this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user));
$this->_unitOfWork->registerRemoved($this->_user);
$this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user));
} }
/*public function testSavedEntityHasIdentityAndIsManaged() /*public function testSavedEntityHasIdentityAndIsManaged()
......
<?php
class CmsPhonenumber extends Doctrine_Record
{
public static function initMetadata($class)
{
$class->mapColumn('user_id', 'integer', 4);
$class->mapColumn('phonenumber', 'string', 50, array('primary' => true));
}
}
...@@ -6,5 +6,8 @@ class CmsUser extends Doctrine_Record ...@@ -6,5 +6,8 @@ class CmsUser extends Doctrine_Record
$class->mapColumn('id', 'integer', 4, array('primary' => true, 'autoincrement' => true)); $class->mapColumn('id', 'integer', 4, array('primary' => true, 'autoincrement' => true));
$class->mapColumn('username', 'string', 255); $class->mapColumn('username', 'string', 255);
$class->mapColumn('name', 'string', 255); $class->mapColumn('name', 'string', 255);
$class->hasMany('CmsPhonenumber as phonenumbers', array(
'local' => 'id', 'foreign' => 'user_id'));
} }
} }
...@@ -77,7 +77,7 @@ class Doctrine_Access_TestCase extends Doctrine_UnitTestCase ...@@ -77,7 +77,7 @@ class Doctrine_Access_TestCase extends Doctrine_UnitTestCase
$user->save(); $user->save();
$user = $this->connection->getMapper('User')->find($user->identifier()); $user = $this->connection->getRepository('User')->find($user->identifier());
$this->assertEqual($user->name, 'Jack'); $this->assertEqual($user->name, 'Jack');
$user['name'] = 'Jack'; $user['name'] = 'Jack';
...@@ -97,7 +97,7 @@ class Doctrine_Access_TestCase extends Doctrine_UnitTestCase ...@@ -97,7 +97,7 @@ class Doctrine_Access_TestCase extends Doctrine_UnitTestCase
$user->save(); $user->save();
$user = $this->connection->getMapper('User')->find($user->identifier()); $user = $this->connection->getRepository('User')->find($user->identifier());
$this->assertEqual($user->name, 'Jack'); $this->assertEqual($user->name, 'Jack');
$user->name = 'Jack'; $user->name = 'Jack';
...@@ -115,7 +115,7 @@ class Doctrine_Access_TestCase extends Doctrine_UnitTestCase ...@@ -115,7 +115,7 @@ class Doctrine_Access_TestCase extends Doctrine_UnitTestCase
$user->save(); $user->save();
$user = $this->connection->getMapper('User')->find($user->identifier()); $user = $this->connection->getRepository('User')->find($user->identifier());
$this->assertEqual($user->get('name'), 'Jack'); $this->assertEqual($user->get('name'), 'Jack');
......
...@@ -168,7 +168,7 @@ class Doctrine_ClassTableInheritance_TestCase extends Doctrine_UnitTestCase ...@@ -168,7 +168,7 @@ class Doctrine_ClassTableInheritance_TestCase extends Doctrine_UnitTestCase
$profiler = new Doctrine_Connection_Profiler(); $profiler = new Doctrine_Connection_Profiler();
$this->conn->addListener($profiler); $this->conn->addListener($profiler);
$record = $this->conn->getMapper('CTITest')->find(1); $record = $this->conn->getRepository('CTITest')->find(1);
$record->age = 11; $record->age = 11;
$record->name = 'Jack'; $record->name = 'Jack';
...@@ -193,7 +193,7 @@ class Doctrine_ClassTableInheritance_TestCase extends Doctrine_UnitTestCase ...@@ -193,7 +193,7 @@ class Doctrine_ClassTableInheritance_TestCase extends Doctrine_UnitTestCase
{ {
$this->conn->clear(); $this->conn->clear();
$record = $this->conn->getMapper('CTITest')->find(1); $record = $this->conn->getRepository('CTITest')->find(1);
$this->assertEqual($record->id, 1); $this->assertEqual($record->id, 1);
$this->assertEqual($record->name, 'Jack'); $this->assertEqual($record->name, 'Jack');
...@@ -209,7 +209,7 @@ class Doctrine_ClassTableInheritance_TestCase extends Doctrine_UnitTestCase ...@@ -209,7 +209,7 @@ class Doctrine_ClassTableInheritance_TestCase extends Doctrine_UnitTestCase
$profiler = new Doctrine_Connection_Profiler(); $profiler = new Doctrine_Connection_Profiler();
$this->conn->addListener($profiler); $this->conn->addListener($profiler);
$record = $this->conn->getMapper('CTITest')->find(1); $record = $this->conn->getRepository('CTITest')->find(1);
$record->delete(); $record->delete();
......
...@@ -53,7 +53,7 @@ class Doctrine_CustomPrimaryKey_TestCase extends Doctrine_UnitTestCase ...@@ -53,7 +53,7 @@ class Doctrine_CustomPrimaryKey_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($c->identifier(), array('uid' => 1)); $this->assertEqual($c->identifier(), array('uid' => 1));
$this->connection->clear(); $this->connection->clear();
$c = $this->connection->getMapper('CustomPK')->find(1); $c = $this->connection->getRepository('CustomPK')->find(1);
$this->assertEqual($c->identifier(), array('uid' => 1)); $this->assertEqual($c->identifier(), array('uid' => 1));
} }
......
...@@ -60,7 +60,7 @@ class Doctrine_DataType_Boolean_TestCase extends Doctrine_UnitTestCase { ...@@ -60,7 +60,7 @@ class Doctrine_DataType_Boolean_TestCase extends Doctrine_UnitTestCase {
$this->connection->clear(); $this->connection->clear();
$test = $test->getMapper()->find($test->id); $test = $test->getRepository()->find($test->id);
$this->assertIdentical($test->is_working, true); $this->assertIdentical($test->is_working, true);
} }
public function testNormalQuerying() { public function testNormalQuerying() {
......
...@@ -172,7 +172,7 @@ class Doctrine_DataType_Enum_TestCase extends Doctrine_UnitTestCase ...@@ -172,7 +172,7 @@ class Doctrine_DataType_Enum_TestCase extends Doctrine_UnitTestCase
public function testFailingRefresh() public function testFailingRefresh()
{ {
$enum = $this->connection->getMapper('EnumTest')->find(1); $enum = $this->connection->getRepository('EnumTest')->find(1);
$this->conn->exec('DELETE FROM enum_test WHERE id = 1'); $this->conn->exec('DELETE FROM enum_test WHERE id = 1');
......
...@@ -197,7 +197,7 @@ class Doctrine_UnitTestCase extends UnitTestCase ...@@ -197,7 +197,7 @@ class Doctrine_UnitTestCase extends UnitTestCase
//echo "exporting : " . var_dump($this->tables); //echo "exporting : " . var_dump($this->tables);
//echo "<br /><br />"; //echo "<br /><br />";
$this->conn->export->exportClasses($this->tables); $this->conn->export->exportClasses($this->tables);
$this->objTable = $this->connection->getMapper('User'); $this->objTable = $this->connection->getRepository('User');
} }
public function prepareData() public function prepareData()
......
...@@ -152,7 +152,7 @@ class Doctrine_Inheritance_Joined_TestCase extends Doctrine_UnitTestCase ...@@ -152,7 +152,7 @@ class Doctrine_Inheritance_Joined_TestCase extends Doctrine_UnitTestCase
public function testDqlQueryJoinsTransparentlyAcrossParents() public function testDqlQueryJoinsTransparentlyAcrossParents()
{ {
$this->_createManager(); $this->_createManager();
$this->conn->getMapper('CTI_Manager')->clear(); $this->conn->clear('CTI_Manager');
$query = $this->conn->createQuery(); $query = $this->conn->createQuery();
$query->parseQuery("SELECT m.* FROM CTI_Manager m"); $query->parseQuery("SELECT m.* FROM CTI_Manager m");
...@@ -167,8 +167,8 @@ class Doctrine_Inheritance_Joined_TestCase extends Doctrine_UnitTestCase ...@@ -167,8 +167,8 @@ class Doctrine_Inheritance_Joined_TestCase extends Doctrine_UnitTestCase
public function testQueryingBaseClassOuterJoinsSubClassesAndReturnsSubclassInstances() public function testQueryingBaseClassOuterJoinsSubClassesAndReturnsSubclassInstances()
{ {
$this->_createManager(); $this->_createManager();
$this->conn->getMapper('CTI_Manager')->clear(); $this->conn->clear('CTI_Manager');
$this->conn->getMapper('CTI_User')->clear(); $this->conn->clear('CTI_User');
$query = $this->conn->createQuery(); $query = $this->conn->createQuery();
$query->parseQuery("SELECT u.* FROM CTI_User u"); $query->parseQuery("SELECT u.* FROM CTI_User u");
......
...@@ -41,9 +41,9 @@ class Doctrine_Query_JoinCondition2_TestCase extends Doctrine_UnitTestCase ...@@ -41,9 +41,9 @@ class Doctrine_Query_JoinCondition2_TestCase extends Doctrine_UnitTestCase
public function prepareData() public function prepareData()
{ {
$this->conn->getMapper('User')->clear(); $this->conn->clear('User');
$this->conn->getMapper('Group')->clear(); $this->conn->clear('Group');
$this->conn->getMapper('Groupuser')->clear(); $this->conn->clear('Groupuser');
$zYne = new User(); $zYne = new User();
$zYne->name = 'zYne'; $zYne->name = 'zYne';
......
...@@ -43,7 +43,7 @@ class Doctrine_Query_MultiJoin_TestCase extends Doctrine_UnitTestCase ...@@ -43,7 +43,7 @@ class Doctrine_Query_MultiJoin_TestCase extends Doctrine_UnitTestCase
$query = new Doctrine_Query($this->connection); $query = new Doctrine_Query($this->connection);
$user = $this->connection->getMapper('User')->find(4); $user = $this->connection->getRepository('User')->find(4);
$album = $this->connection->create('Album'); $album = $this->connection->create('Album');
...@@ -73,7 +73,7 @@ class Doctrine_Query_MultiJoin_TestCase extends Doctrine_UnitTestCase ...@@ -73,7 +73,7 @@ class Doctrine_Query_MultiJoin_TestCase extends Doctrine_UnitTestCase
$this->assertEqual(count($user->Album[1]->Song), 4); $this->assertEqual(count($user->Album[1]->Song), 4);
$user = $this->connection->getMapper('User')->find(5); $user = $this->connection->getRepository('User')->find(5);
$user->Album[0]->name = 'Clayman'; $user->Album[0]->name = 'Clayman';
$user->Album[1]->name = 'Colony'; $user->Album[1]->name = 'Colony';
...@@ -122,7 +122,7 @@ class Doctrine_Query_MultiJoin_TestCase extends Doctrine_UnitTestCase ...@@ -122,7 +122,7 @@ class Doctrine_Query_MultiJoin_TestCase extends Doctrine_UnitTestCase
public function testInitializeMoreData() public function testInitializeMoreData()
{ {
$user = $this->connection->getMapper('User')->find(4); $user = $this->connection->getRepository('User')->find(4);
$user->Book[0]->name = 'The Prince'; $user->Book[0]->name = 'The Prince';
$user->Book[0]->Author[0]->name = 'Niccolo Machiavelli'; $user->Book[0]->Author[0]->name = 'Niccolo Machiavelli';
$user->Book[0]->Author[1]->name = 'Someone'; $user->Book[0]->Author[1]->name = 'Someone';
...@@ -133,7 +133,7 @@ class Doctrine_Query_MultiJoin_TestCase extends Doctrine_UnitTestCase ...@@ -133,7 +133,7 @@ class Doctrine_Query_MultiJoin_TestCase extends Doctrine_UnitTestCase
$user->save(); $user->save();
$user = $this->connection->getMapper('User')->find(5); $user = $this->connection->getRepository('User')->find(5);
$user->Book[0]->name = 'Zadig'; $user->Book[0]->name = 'Zadig';
$user->Book[0]->Author[0]->name = 'Voltaire'; $user->Book[0]->Author[0]->name = 'Voltaire';
$user->Book[0]->Author[1]->name = 'Someone'; $user->Book[0]->Author[1]->name = 'Someone';
......
...@@ -60,7 +60,7 @@ class Doctrine_Query_ReferenceModel_TestCase extends Doctrine_UnitTestCase { ...@@ -60,7 +60,7 @@ class Doctrine_Query_ReferenceModel_TestCase extends Doctrine_UnitTestCase {
$this->connection->unitOfWork->saveAll(); $this->connection->unitOfWork->saveAll();
$this->connection->clear(); $this->connection->clear();
$category = $category->getMapper()->find($category->id); $category = $category->getRepository()->find($category->id);
$this->assertEqual($category->name, 'Root'); $this->assertEqual($category->name, 'Root');
$this->assertEqual($category->Subcategory[0]->name, 'Sub 1'); $this->assertEqual($category->Subcategory[0]->name, 'Sub 1');
......
...@@ -62,6 +62,6 @@ class Doctrine_Query_Registry_TestCase extends Doctrine_UnitTestCase ...@@ -62,6 +62,6 @@ class Doctrine_Query_Registry_TestCase extends Doctrine_UnitTestCase
$user = new User(); $user = new User();
$user->getMapper()->executeNamedQuery('User.all'); $user->getEntityManager()->executeNamedQuery('User.all');
} }
} }
This diff is collapsed.
...@@ -117,7 +117,7 @@ class Doctrine_Relation_Nest_TestCase extends Doctrine_UnitTestCase ...@@ -117,7 +117,7 @@ class Doctrine_Relation_Nest_TestCase extends Doctrine_UnitTestCase
$this->connection->clear(); $this->connection->clear();
$e = $e->getMapper()->find($e->id); $e = $e->getRepository()->find($e->id);
$count = count($this->conn); $count = count($this->conn);
......
...@@ -83,7 +83,7 @@ class Doctrine_Table_TestCase extends Doctrine_UnitTestCase ...@@ -83,7 +83,7 @@ class Doctrine_Table_TestCase extends Doctrine_UnitTestCase
$this->connection->clear(); $this->connection->clear();
$t = $this->connection->getMapper('FieldNameTest')->find(1); $t = $this->connection->getRepository('FieldNameTest')->find(1);
$this->assertEqual($t->someColumn, 'abc'); $this->assertEqual($t->someColumn, 'abc');
$this->assertEqual($t->someEnum, 'php'); $this->assertEqual($t->someEnum, 'php');
......
...@@ -37,7 +37,7 @@ class Doctrine_Ticket_626D_TestCase extends Doctrine_UnitTestCase ...@@ -37,7 +37,7 @@ class Doctrine_Ticket_626D_TestCase extends Doctrine_UnitTestCase
$student1 = $this->newStudent('T626D_Student1', '07090002', 'First Student'); $student1 = $this->newStudent('T626D_Student1', '07090002', 'First Student');
try { try {
$student = $this->conn->getMapper('T626D_Student1')->find('07090002'); $student = $this->conn->getRepository('T626D_Student1')->find('07090002');
$this->pass(); $this->pass();
} catch (Exception $e) { } catch (Exception $e) {
$this->fail($e->__toString()); $this->fail($e->__toString());
......
...@@ -140,7 +140,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase ...@@ -140,7 +140,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase
public function testValidate() public function testValidate()
{ {
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL); $this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
$user = $this->connection->getMapper('User')->find(4); $user = $this->connection->getRepository('User')->find(4);
$set = array('password' => 'this is an example of too long password', $set = array('password' => 'this is an example of too long password',
'loginname' => 'this is an example of too long loginname', 'loginname' => 'this is an example of too long loginname',
...@@ -200,7 +200,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase ...@@ -200,7 +200,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase
public function testSave() public function testSave()
{ {
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL); $this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
$user = $this->connection->getMapper("User")->find(4); $user = $this->connection->getRepository("User")->find(4);
try { try {
$user->name = "this is an example of too long name not very good example but an example nevertheless"; $user->name = "this is an example of too long name not very good example but an example nevertheless";
$user->save(); $user->save();
...@@ -260,7 +260,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase ...@@ -260,7 +260,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase
} }
// Tests validateOnUpdate() // Tests validateOnUpdate()
$user = $this->connection->getMapper("User")->find(4); $user = $this->connection->getRepository("User")->find(4);
try { try {
$user->name = "The Saint"; // Set correct name $user->name = "The Saint"; // Set correct name
$user->password = "Top Secret"; // Set correct password $user->password = "Top Secret"; // Set correct password
...@@ -337,7 +337,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase ...@@ -337,7 +337,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase
$r->identifier = '1234'; $r->identifier = '1234';
$r->save(); $r->save();
$r = $this->connection->getMapper('ValidatorTest_Person')->findAll()->getFirst(); $r = $this->connection->getRepository('ValidatorTest_Person')->findAll()->getFirst();
$r->identifier = 1234; $r->identifier = 1234;
try { try {
$r->save(); $r->save();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment