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