Commit 2ec4cc5c authored by romanb's avatar romanb

[2.0] More cleanups for recent lazy-loading implementation and minor object...

[2.0] More cleanups for recent lazy-loading implementation and minor object hydration improvements and cleanups. Collection refactoring part I for ticket #2352.
parent 62446f0f
...@@ -21,11 +21,7 @@ ...@@ -21,11 +21,7 @@
namespace Doctrine\Common\Collections; namespace Doctrine\Common\Collections;
use \Closure; use \Closure, \ArrayIterator;
use \Countable;
use \IteratorAggregate;
use \ArrayAccess;
use \ArrayIterator;
/** /**
* A Collection is a thin wrapper around a php array. Like a php array it is essentially * A Collection is a thin wrapper around a php array. Like a php array it is essentially
...@@ -34,7 +30,7 @@ use \ArrayIterator; ...@@ -34,7 +30,7 @@ use \ArrayIterator;
* @author Roman S. Borschel <roman@code-factory.org> * @author Roman S. Borschel <roman@code-factory.org>
* @since 2.0 * @since 2.0
*/ */
class Collection implements Countable, IteratorAggregate, ArrayAccess class ArrayCollection implements ICollection
{ {
/** /**
* An array containing the entries of this collection. * An array containing the entries of this collection.
...@@ -42,10 +38,10 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess ...@@ -42,10 +38,10 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
* *
* @var array * @var array
*/ */
protected $_elements; private $_elements;
/** /**
* Initializes a new Collection. * Initializes a new ArrayCollection.
* *
* @param array $elements * @param array $elements
*/ */
...@@ -63,6 +59,11 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess ...@@ -63,6 +59,11 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
{ {
return $this->_elements; return $this->_elements;
} }
public function toArray()
{
return $this->_elements;
}
/** /**
* Gets the first element in the collection. * Gets the first element in the collection.
...@@ -249,7 +250,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess ...@@ -249,7 +250,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
* *
* @return array * @return array
*/ */
public function getElements() public function getValues()
{ {
return array_values($this->_elements); return array_values($this->_elements);
} }
...@@ -323,7 +324,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess ...@@ -323,7 +324,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
*/ */
public function map(Closure $func) public function map(Closure $func)
{ {
return new Collection(array_map($func, $this->_elements)); return new ArrayCollection(array_map($func, $this->_elements));
} }
/** /**
...@@ -335,7 +336,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess ...@@ -335,7 +336,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
*/ */
public function filter(Closure $p) public function filter(Closure $p)
{ {
return new Collection(array_filter($this->_elements, $p)); return new ArrayCollection(array_filter($this->_elements, $p));
} }
/** /**
...@@ -374,7 +375,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess ...@@ -374,7 +375,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
$coll2[$key] = $element; $coll2[$key] = $element;
} }
} }
return array(new Collection($coll1), new Collection($coll2)); return array(new ArrayCollection($coll1), new ArrayCollection($coll2));
} }
/** /**
......
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
namespace Doctrine\ORM\Internal\Hydration; namespace Doctrine\ORM\Internal\Hydration;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection,
use Doctrine\DBAL\Types\Type; Doctrine\DBAL\Types\Type,
use Doctrine\Common\DoctrineException; Doctrine\Common\DoctrineException;
/** /**
* Base class for all hydrators. A hydrator is a class that provides some form * Base class for all hydrators. A hydrator is a class that provides some form
...@@ -151,7 +151,7 @@ abstract class AbstractHydrator ...@@ -151,7 +151,7 @@ abstract class AbstractHydrator
*/ */
protected function _hydrateRow(array &$data, array &$cache, &$result) protected function _hydrateRow(array &$data, array &$cache, &$result)
{ {
throw new DoctrineException("_hydrateRow() not implemented for this hydrator."); throw new DoctrineException("_hydrateRow() not implemented by this hydrator.");
} }
/** /**
......
...@@ -111,6 +111,11 @@ class ClassMetadataFactory ...@@ -111,6 +111,11 @@ class ClassMetadataFactory
} }
return $this->_loadedMetadata[$className]; return $this->_loadedMetadata[$className];
} }
public function hasMetadataFor($className)
{
return isset($this->_loadedMetadata[$className]);
}
/** /**
* Sets the metadata descriptor for a specific class. * Sets the metadata descriptor for a specific class.
......
This diff is collapsed.
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
namespace Doctrine\ORM\Persisters; namespace Doctrine\ORM\Persisters;
use Doctrine\Common\DoctrineException; use Doctrine\Common\DoctrineException;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
...@@ -490,7 +491,8 @@ class StandardEntityPersister ...@@ -490,7 +491,8 @@ class StandardEntityPersister
} }
} else { } else {
// Inject collection // Inject collection
$coll = new PersistentCollection($this->_em, $this->_em->getClassMetadata($assoc->targetEntityName)); $coll = new PersistentCollection($this->_em, $this->_em->getClassMetadata($assoc->targetEntityName),
new ArrayCollection);
$coll->setOwner($entity, $assoc); $coll->setOwner($entity, $assoc);
$this->_class->reflFields[$field]->setValue($entity, $coll); $this->_class->reflFields[$field]->setValue($entity, $coll);
if ($assoc->isLazilyFetched()) { if ($assoc->isLazilyFetched()) {
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
*/ */
namespace Doctrine\ORM\Proxy; namespace Doctrine\ORM\Proxy;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadata;
...@@ -85,12 +86,15 @@ class ProxyClassGenerator ...@@ -85,12 +86,15 @@ class ProxyClassGenerator
protected function _generateClass($originalClassName, $proxyClassName, $file) protected function _generateClass($originalClassName, $proxyClassName, $file)
{ {
$class = $this->_em->getClassMetadata($originalClassName);
$proxyFullyQualifiedClassName = self::$_ns . $proxyClassName; $proxyFullyQualifiedClassName = self::$_ns . $proxyClassName;
if ($this->_em->getMetadataFactory()->hasMetadataFor($proxyFullyQualifiedClassName)) {
return $proxyFullyQualifiedClassName;
}
$class = $this->_em->getClassMetadata($originalClassName); $class = $this->_em->getClassMetadata($originalClassName);
$this->_em->getMetadataFactory()->setMetadataFor($proxyFullyQualifiedClassName, $class); $this->_em->getMetadataFactory()->setMetadataFor($proxyFullyQualifiedClassName, $class);
if (class_exists($proxyFullyQualifiedClassName, false)) { if (class_exists($proxyFullyQualifiedClassName, false)) {
return $proxyFullyQualifiedClassName; return $proxyFullyQualifiedClassName;
} }
...@@ -214,7 +218,7 @@ namespace Doctrine\Generated\Proxies { ...@@ -214,7 +218,7 @@ namespace Doctrine\Generated\Proxies {
public function __sleep() { public function __sleep() {
if (!$this->_loaded) { if (!$this->_loaded) {
throw new RuntimeException("Not fully loaded proxy can not be serialized."); throw new \RuntimeException("Not fully loaded proxy can not be serialized.");
} }
<sleepImpl> <sleepImpl>
} }
...@@ -252,7 +256,7 @@ namespace Doctrine\Generated\Proxies { ...@@ -252,7 +256,7 @@ namespace Doctrine\Generated\Proxies {
public function __sleep() { public function __sleep() {
if (!$this->_loaded) { if (!$this->_loaded) {
throw new RuntimeException("Not fully loaded proxy can not be serialized."); throw new \RuntimeException("Not fully loaded proxy can not be serialized.");
} }
<sleepImpl> <sleepImpl>
} }
......
...@@ -74,6 +74,8 @@ final class Query extends AbstractQuery ...@@ -74,6 +74,8 @@ final class Query extends AbstractQuery
* @var string * @var string
*/ */
const HINT_INCLUDE_META_COLUMNS = 'doctrine.includeMetaColumns'; const HINT_INCLUDE_META_COLUMNS = 'doctrine.includeMetaColumns';
//const HINT_READ_ONLY = 'doctrine.readOnly';
/** /**
* @var integer $_state The current state of this query. * @var integer $_state The current state of this query.
......
...@@ -21,17 +21,13 @@ ...@@ -21,17 +21,13 @@
namespace Doctrine\ORM; namespace Doctrine\ORM;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection,
use Doctrine\Common\DoctrineException; Doctrine\Common\Collections\ICollection,
use Doctrine\Common\PropertyChangedListener; Doctrine\Common\DoctrineException,
use Doctrine\ORM\Events; Doctrine\Common\PropertyChangedListener,
use Doctrine\ORM\Event\LifecycleEventArgs; Doctrine\ORM\Event\LifecycleEventArgs,
use Doctrine\ORM\Internal\CommitOrderCalculator; Doctrine\ORM\Internal\CommitOrderCalculator,
use Doctrine\ORM\Internal\CommitOrderNode; Doctrine\ORM\Internal\CommitOrderNode;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Mapping;
use Doctrine\ORM\Persisters;
use Doctrine\ORM\EntityManager;
/** /**
* The UnitOfWork is responsible for tracking changes to objects during an * The UnitOfWork is responsible for tracking changes to objects during an
...@@ -468,13 +464,13 @@ class UnitOfWork implements PropertyChangedListener ...@@ -468,13 +464,13 @@ class UnitOfWork implements PropertyChangedListener
if ($class->isCollectionValuedAssociation($name) && $actualData[$name] !== null if ($class->isCollectionValuedAssociation($name) && $actualData[$name] !== null
&& ! ($actualData[$name] instanceof PersistentCollection)) { && ! ($actualData[$name] instanceof PersistentCollection)) {
// If $actualData[$name] is Collection then unwrap the array // If $actualData[$name] is Collection then unwrap the array
if ($actualData[$name] instanceof Collection) { if ( ! $actualData[$name] instanceof ArrayCollection) {
$actualData[$name] = $actualData[$name]->unwrap(); $actualData[$name] = new ArrayCollection($actualData[$name]);
} }
$assoc = $class->associationMappings[$name]; $assoc = $class->associationMappings[$name];
// Inject PersistentCollection // Inject PersistentCollection
$coll = new PersistentCollection($this->_em, $this->_em->getClassMetadata($assoc->targetEntityName), $coll = new PersistentCollection($this->_em, $this->_em->getClassMetadata(
$actualData[$name] ? $actualData[$name] : array()); $assoc->targetEntityName), $actualData[$name]);
$coll->setOwner($entity, $assoc); $coll->setOwner($entity, $assoc);
$coll->setDirty( ! $coll->isEmpty()); $coll->setDirty( ! $coll->isEmpty());
$class->reflFields[$name]->setValue($entity, $coll); $class->reflFields[$name]->setValue($entity, $coll);
...@@ -1326,7 +1322,8 @@ class UnitOfWork implements PropertyChangedListener ...@@ -1326,7 +1322,8 @@ class UnitOfWork implements PropertyChangedListener
} else { } else {
//TODO: Only do this when allowPartialObjects == false? //TODO: Only do this when allowPartialObjects == false?
$coll = new PersistentCollection($this->_em, $coll = new PersistentCollection($this->_em,
$this->_em->getClassMetadata($assoc2->targetEntityName) $this->_em->getClassMetadata($assoc2->targetEntityName),
new ArrayCollection
); );
$coll->setOwner($managedCopy, $assoc2); $coll->setOwner($managedCopy, $assoc2);
$coll->setInitialized($assoc2->isCascadeMerge); $coll->setInitialized($assoc2->isCascadeMerge);
...@@ -1458,7 +1455,7 @@ class UnitOfWork implements PropertyChangedListener ...@@ -1458,7 +1455,7 @@ class UnitOfWork implements PropertyChangedListener
continue; continue;
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity);
if ($relatedEntities instanceof Collection) { if ($relatedEntities instanceof ICollection) {
foreach ($relatedEntities as $relatedEntity) { foreach ($relatedEntities as $relatedEntity) {
$this->_doRefresh($relatedEntity, $visited); $this->_doRefresh($relatedEntity, $visited);
} }
...@@ -1482,7 +1479,7 @@ class UnitOfWork implements PropertyChangedListener ...@@ -1482,7 +1479,7 @@ class UnitOfWork implements PropertyChangedListener
continue; continue;
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity);
if ($relatedEntities instanceof Collection) { if ($relatedEntities instanceof ICollection) {
foreach ($relatedEntities as $relatedEntity) { foreach ($relatedEntities as $relatedEntity) {
$this->_doDetach($relatedEntity, $visited); $this->_doDetach($relatedEntity, $visited);
} }
...@@ -1507,7 +1504,7 @@ class UnitOfWork implements PropertyChangedListener ...@@ -1507,7 +1504,7 @@ class UnitOfWork implements PropertyChangedListener
continue; continue;
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity);
if ($relatedEntities instanceof Collection) { if ($relatedEntities instanceof ICollection) {
foreach ($relatedEntities as $relatedEntity) { foreach ($relatedEntities as $relatedEntity) {
$this->_doMerge($relatedEntity, $visited, $managedCopy, $assocMapping); $this->_doMerge($relatedEntity, $visited, $managedCopy, $assocMapping);
} }
...@@ -1532,7 +1529,7 @@ class UnitOfWork implements PropertyChangedListener ...@@ -1532,7 +1529,7 @@ class UnitOfWork implements PropertyChangedListener
continue; continue;
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity);
if (($relatedEntities instanceof Collection || is_array($relatedEntities))) { if (($relatedEntities instanceof ICollection || is_array($relatedEntities))) {
foreach ($relatedEntities as $relatedEntity) { foreach ($relatedEntities as $relatedEntity) {
$this->_doPersist($relatedEntity, $visited); $this->_doPersist($relatedEntity, $visited);
} }
...@@ -1557,7 +1554,7 @@ class UnitOfWork implements PropertyChangedListener ...@@ -1557,7 +1554,7 @@ class UnitOfWork implements PropertyChangedListener
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName] $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]
->getValue($entity); ->getValue($entity);
if ($relatedEntities instanceof Collection || is_array($relatedEntities)) { if ($relatedEntities instanceof ICollection || is_array($relatedEntities)) {
foreach ($relatedEntities as $relatedEntity) { foreach ($relatedEntities as $relatedEntity) {
$this->_doRemove($relatedEntity, $visited); $this->_doRemove($relatedEntity, $visited);
} }
......
...@@ -12,7 +12,7 @@ class CollectionTest extends \Doctrine\Tests\DoctrineTestCase ...@@ -12,7 +12,7 @@ class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
protected function setUp() protected function setUp()
{ {
$this->_coll = new \Doctrine\Common\Collections\Collection; $this->_coll = new \Doctrine\Common\Collections\ArrayCollection;
} }
public function testIssetAndUnset() public function testIssetAndUnset()
...@@ -114,11 +114,11 @@ class CollectionTest extends \Doctrine\Tests\DoctrineTestCase ...@@ -114,11 +114,11 @@ class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
$this->assertEquals(array(0, 1), $this->_coll->getKeys()); $this->assertEquals(array(0, 1), $this->_coll->getKeys());
} }
public function testGetElements() public function testGetValues()
{ {
$this->_coll[] = 'one'; $this->_coll[] = 'one';
$this->_coll[] = 'two'; $this->_coll[] = 'two';
$this->assertEquals(array('one', 'two'), $this->_coll->getElements()); $this->assertEquals(array('one', 'two'), $this->_coll->getValues());
} }
public function testCount() public function testCount()
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Doctrine\Tests\Models\CMS; namespace Doctrine\Tests\Models\CMS;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection;
/** /**
* @Entity * @Entity
...@@ -49,9 +49,9 @@ class CmsUser ...@@ -49,9 +49,9 @@ class CmsUser
public $groups; public $groups;
public function __construct() { public function __construct() {
$this->phonenumbers = new Collection; $this->phonenumbers = new ArrayCollection;
$this->articles = new Collection; $this->articles = new ArrayCollection;
$this->groups = new Collection; $this->groups = new ArrayCollection;
} }
public function getId() { public function getId() {
......
...@@ -62,7 +62,7 @@ class CompanyPerson ...@@ -62,7 +62,7 @@ class CompanyPerson
public function addFriend(CompanyPerson $friend) { public function addFriend(CompanyPerson $friend) {
if ( ! $this->friends) { if ( ! $this->friends) {
$this->friends = new \Doctrine\Common\Collections\Collection; $this->friends = new \Doctrine\Common\Collections\ArrayCollection;
} }
if ( ! $this->friends->contains($friend)) { if ( ! $this->friends->contains($friend)) {
$this->friends->add($friend); $this->friends->add($friend);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Doctrine\Tests\Models\ECommerce; namespace Doctrine\Tests\Models\ECommerce;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection;
/** /**
* ECommerceCart * ECommerceCart
...@@ -42,7 +42,7 @@ class ECommerceCart ...@@ -42,7 +42,7 @@ class ECommerceCart
public function __construct() public function __construct()
{ {
$this->products = new Collection; $this->products = new ArrayCollection;
} }
public function getId() { public function getId() {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Doctrine\Tests\Models\ECommerce; namespace Doctrine\Tests\Models\ECommerce;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection;
/** /**
* ECommerceCategory * ECommerceCategory
...@@ -44,8 +44,8 @@ class ECommerceCategory ...@@ -44,8 +44,8 @@ class ECommerceCategory
public function __construct() public function __construct()
{ {
$this->products = new Collection(); $this->products = new ArrayCollection();
$this->children = new Collection(); $this->children = new ArrayCollection();
} }
public function getId() public function getId()
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Doctrine\Tests\Models\ECommerce; namespace Doctrine\Tests\Models\ECommerce;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection;
/** /**
* ECommerceProduct * ECommerceProduct
...@@ -57,9 +57,9 @@ class ECommerceProduct ...@@ -57,9 +57,9 @@ class ECommerceProduct
public function __construct() public function __construct()
{ {
$this->features = new Collection; $this->features = new ArrayCollection;
$this->categories = new Collection; $this->categories = new ArrayCollection;
$this->related = new Collection; $this->related = new ArrayCollection;
} }
public function getId() public function getId()
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Doctrine\Tests\ORM\Functional; namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ICollection;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
...@@ -36,18 +36,8 @@ class AbstractManyToManyAssociationTestCase extends \Doctrine\Tests\OrmFunctiona ...@@ -36,18 +36,8 @@ class AbstractManyToManyAssociationTestCase extends \Doctrine\Tests\OrmFunctiona
->fetchAll()); ->fetchAll());
} }
public function assertCollectionEquals(Collection $first, Collection $second) public function assertCollectionEquals(ICollection $first, ICollection $second)
{ {
return $first->forAll(function($k, $e) use($second) { return $second->contains($e); }); return $first->forAll(function($k, $e) use($second) { return $second->contains($e); });
/*if (count($first) != count($second)) {
return false;
}
foreach ($first as $element) {
if (!$second->contains($element)) {
return false;
}
}
return true;*/
} }
} }
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace Doctrine\Tests\ORM\Functional; namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Collections\Collection;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct; use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\Tests\Models\ECommerce\ECommerceCategory; use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
use Doctrine\ORM\Mapping\AssociationMapping; use Doctrine\ORM\Mapping\AssociationMapping;
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace Doctrine\Tests\ORM\Functional; namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Collections\Collection;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct; use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\ORM\Mapping\AssociationMapping; use Doctrine\ORM\Mapping\AssociationMapping;
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace Doctrine\Tests\ORM\Functional; namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Collections\Collection;
use Doctrine\Tests\Models\ECommerce\ECommerceCart; use Doctrine\Tests\Models\ECommerce\ECommerceCart;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct; use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\ORM\Mapping\AssociationMapping; use Doctrine\ORM\Mapping\AssociationMapping;
......
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