Commit 36763dad authored by romanb's avatar romanb

A little progress on the UnitOfWork.

parent 0ac97e7a
...@@ -9,6 +9,7 @@ namespace Doctrine\Common\Collections; ...@@ -9,6 +9,7 @@ namespace Doctrine\Common\Collections;
use \Countable; use \Countable;
use \IteratorAggregate; use \IteratorAggregate;
use \ArrayAccess; use \ArrayAccess;
use \ArrayIterator;
/** /**
* A Collection is a wrapper around a php array and just like a php array a * A Collection is a wrapper around a php array and just like a php array a
...@@ -88,6 +89,22 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess ...@@ -88,6 +89,22 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
return $removed; return $removed;
} }
/**
* Removes the specified element from the collection, if it is found.
*
* @param mixed $element
* @return boolean
*/
public function removeElement($element)
{
$key = array_search($element, $this->_data, true);
if ($key !== false) {
unset($this->_data[$key]);
return true;
}
return false;
}
/** /**
* @see containsKey() * @see containsKey()
*/ */
...@@ -174,7 +191,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess ...@@ -174,7 +191,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
* Tests for the existance of an element that satisfies the given predicate. * Tests for the existance of an element that satisfies the given predicate.
* *
* @param function $func * @param function $func
* @return boolean TRUE if the predicate is TRUE for at least one element, FALSe otherwise. * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
*/ */
public function exists(Closure $func) { public function exists(Closure $func) {
foreach ($this->_data as $key => $element) foreach ($this->_data as $key => $element)
...@@ -191,7 +208,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess ...@@ -191,7 +208,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
*/ */
public function containsAll($otherColl) public function containsAll($otherColl)
{ {
throw new Doctrine_Exception("Not yet implemented."); throw new DoctrineException("Not yet implemented.");
} }
/** /**
......
...@@ -495,7 +495,7 @@ class Connection ...@@ -495,7 +495,7 @@ class Connection
*/ */
public function prepare($statement) public function prepare($statement)
{ {
echo $statement; echo $statement . PHP_EOL;
$this->connect(); $this->connect();
try { try {
return $this->_conn->prepare($statement); return $this->_conn->prepare($statement);
...@@ -561,6 +561,7 @@ class Connection ...@@ -561,6 +561,7 @@ class Connection
$this->connect(); $this->connect();
try { try {
if ( ! empty($params)) { if ( ! empty($params)) {
//var_dump($params);
$stmt = $this->prepare($query); $stmt = $this->prepare($query);
$stmt->execute($params); $stmt->execute($params);
return $stmt->rowCount(); return $stmt->rowCount();
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
* most method calls to the EntityManager. * most method calls to the EntityManager.
* *
* @since 2.0 * @since 2.0
* @todo Any takers for this one? Needs a rewrite.
*/ */
class Doctrine_ORM_ActiveEntity class Doctrine_ORM_ActiveEntity
{ {
......
...@@ -21,11 +21,13 @@ ...@@ -21,11 +21,13 @@
namespace Doctrine\ORM; namespace Doctrine\ORM;
use Doctrine\ORM\Mapping\AssociationMapping;
/** /**
* A persistent collection wrapper. * A persistent collection wrapper.
* *
* A PersistentCollection represents a collection of entities. Collections of * A PersistentCollection represents a collection of elements that have persistent state.
* entities represent only the associations (links) to those entities. * Collections of entities represent only the associations (links) to those entities.
* That means, if the collection is part of a many-many mapping and you remove * That means, if the collection is part of a many-many mapping and you remove
* entities from the collection, only the links in the xref table are removed (on flush). * entities from the collection, only the links in the xref table are removed (on flush).
* Similarly, if you remove entities from a collection that is part of a one-many * Similarly, if you remove entities from a collection that is part of a one-many
...@@ -104,13 +106,17 @@ final class Collection extends \Doctrine\Common\Collections\Collection ...@@ -104,13 +106,17 @@ final class Collection extends \Doctrine\Common\Collections\Collection
*/ */
private $_hydrationFlag; private $_hydrationFlag;
/**
* The class descriptor of the owning entity.
*/
private $_ownerClass; private $_ownerClass;
/** /**
* Creates a new persistent collection. * Creates a new persistent collection.
*/ */
public function __construct(EntityManager $em, $entityBaseType, $keyField = null) public function __construct(EntityManager $em, $entityBaseType, array $data = array(), $keyField = null)
{ {
parent::__construct($data);
$this->_entityBaseType = $entityBaseType; $this->_entityBaseType = $entityBaseType;
$this->_em = $em; $this->_em = $em;
$this->_ownerClass = $em->getClassMetadata($entityBaseType); $this->_ownerClass = $em->getClassMetadata($entityBaseType);
...@@ -151,7 +157,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection ...@@ -151,7 +157,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection
* @param object $entity * @param object $entity
* @param AssociationMapping $relation * @param AssociationMapping $relation
*/ */
public function _setOwner($entity, \Doctrine\ORM\Mapping\AssociationMapping $relation) public function _setOwner($entity, AssociationMapping $relation)
{ {
$this->_owner = $entity; $this->_owner = $entity;
$this->_association = $relation; $this->_association = $relation;
...@@ -180,7 +186,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection ...@@ -180,7 +186,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection
} }
/** /**
* Removes an entity from the collection. * Removes an element from the collection.
* *
* @param mixed $key * @param mixed $key
* @return boolean * @return boolean
...@@ -215,23 +221,23 @@ final class Collection extends \Doctrine\Common\Collections\Collection ...@@ -215,23 +221,23 @@ final class Collection extends \Doctrine\Common\Collections\Collection
} }
/** /**
* Adds an entry to the collection. * Adds an element to the collection.
* *
* @param mixed $value * @param mixed $value
* @param string $key * @param string $key
* @return boolean * @return TRUE
* @override * @override
*/ */
public function add($value) public function add($value)
{ {
$result = parent::add($value); $result = parent::add($value);
if ( ! $result) return $result; // EARLY EXIT if ( ! $result) return $result; // EARLY EXIT
if ($this->_hydrationFlag) { if ($this->_hydrationFlag) {
if ($this->_backRefFieldName) { if ($this->_backRefFieldName) {
// set back reference to owner // set back reference to owner
$this->_ownerClass->getReflectionProperty( $this->_ownerClass->getReflectionProperty($this->_backRefFieldName)
$this->_backRefFieldName)->setValue($value, $this->_owner); ->setValue($value, $this->_owner);
} }
} else { } else {
//TODO: Register collection as dirty with the UoW if necessary //TODO: Register collection as dirty with the UoW if necessary
...@@ -295,27 +301,6 @@ final class Collection extends \Doctrine\Common\Collections\Collection ...@@ -295,27 +301,6 @@ final class Collection extends \Doctrine\Common\Collections\Collection
return $this->_snapshot; return $this->_snapshot;
} }
/**
* INTERNAL:
* Processes the difference of the last snapshot and the current data.
*
* an example:
* Snapshot with the objects 1, 2 and 4
* Current data with objects 2, 3 and 5
*
* The process would remove objects 1 and 4
*
* @return Doctrine_Collection
* @todo Move elsewhere
*/
public function processDiff()
{
foreach (array_udiff($this->_snapshot, $this->_data, array($this, "_compareRecords")) as $record) {
$record->delete();
}
return $this;
}
/** /**
* INTERNAL: * INTERNAL:
* getDeleteDiff * getDeleteDiff
...@@ -324,7 +309,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection ...@@ -324,7 +309,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection
*/ */
public function getDeleteDiff() public function getDeleteDiff()
{ {
return array_udiff($this->_snapshot, $this->_data, array($this, "_compareRecords")); return array_udiff($this->_snapshot, $this->_data, array($this, '_compareRecords'));
} }
/** /**
...@@ -334,7 +319,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection ...@@ -334,7 +319,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection
*/ */
public function getInsertDiff() public function getInsertDiff()
{ {
return array_udiff($this->_data, $this->_snapshot, array($this, "_compareRecords")); return array_udiff($this->_data, $this->_snapshot, array($this, '_compareRecords'));
} }
/** /**
...@@ -377,8 +362,10 @@ final class Collection extends \Doctrine\Common\Collections\Collection ...@@ -377,8 +362,10 @@ final class Collection extends \Doctrine\Common\Collections\Collection
private function _changed() private function _changed()
{ {
/*if ( ! $this->_em->getUnitOfWork()->isCollectionScheduledForUpdate($this)) { if ( ! $this->_em->getUnitOfWork()->isCollectionScheduledForUpdate($this)) {
$this->_em->getUnitOfWork()->scheduleCollectionUpdate($this); //var_dump(get_class($this->_snapshot[0]));
}*/ //echo "NOT!";
//$this->_em->getUnitOfWork()->scheduleCollectionUpdate($this);
}
} }
} }
...@@ -54,7 +54,7 @@ class ObjectHydrator extends AbstractHydrator ...@@ -54,7 +54,7 @@ class ObjectHydrator extends AbstractHydrator
if ($this->_parserResult->isMixedQuery()) { if ($this->_parserResult->isMixedQuery()) {
$result = array(); $result = array();
} else { } else {
$result = new \Doctrine\ORM\Collection($this->_em, $this->_rootEntityName); $result = new \Doctrine\Common\Collections\Collection;
} }
$cache = array(); $cache = array();
...@@ -66,7 +66,7 @@ class ObjectHydrator extends AbstractHydrator ...@@ -66,7 +66,7 @@ class ObjectHydrator extends AbstractHydrator
foreach ($this->_collections as $coll) { foreach ($this->_collections as $coll) {
$coll->_takeSnapshot(); $coll->_takeSnapshot();
$coll->_setHydrationFlag(false); $coll->_setHydrationFlag(false);
$this->_uow->addManagedCollection($coll); //$this->_uow->addManagedCollection($coll);
} }
// Clean up // Clean up
...@@ -105,7 +105,7 @@ class ObjectHydrator extends AbstractHydrator ...@@ -105,7 +105,7 @@ class ObjectHydrator extends AbstractHydrator
if ( ! is_object($coll)) { if ( ! is_object($coll)) {
end($coll); end($coll);
$this->_resultPointers[$dqlAlias] =& $coll[key($coll)]; $this->_resultPointers[$dqlAlias] =& $coll[key($coll)];
} else if ($coll instanceof \Doctrine\ORM\Collection) { } else if ($coll instanceof \Doctrine\Common\Collections\Collection) {
if (count($coll) > 0) { if (count($coll) > 0) {
$this->_resultPointers[$dqlAlias] = $coll->last(); $this->_resultPointers[$dqlAlias] = $coll->last();
} }
......
<?php <?php
class Doctrine_ORM_Persisters_AbstractCollectionPersister namespace Doctrine\ORM\Persisters;
use Doctrine\ORM\Collection;
class AbstractCollectionPersister
{ {
public function recreate(Doctrine_Collection $coll) public function recreate(Doctrine_Collection $coll)
...@@ -8,7 +12,6 @@ class Doctrine_ORM_Persisters_AbstractCollectionPersister ...@@ -8,7 +12,6 @@ class Doctrine_ORM_Persisters_AbstractCollectionPersister
if ($coll->getRelation()->isInverseSide()) { if ($coll->getRelation()->isInverseSide()) {
return; return;
} }
//... //...
} }
...@@ -17,32 +20,39 @@ class Doctrine_ORM_Persisters_AbstractCollectionPersister ...@@ -17,32 +20,39 @@ class Doctrine_ORM_Persisters_AbstractCollectionPersister
if ($coll->getRelation()->isInverseSide()) { if ($coll->getRelation()->isInverseSide()) {
return; return;
} }
//... //...
if ($coll->getRelation() instanceof Doctrine_Association_OneToManyMapping) {
//...
} else if ($coll->getRelation() instanceof Doctrine_Association_ManyToManyMapping) {
//...
}
} }
/* collection update actions */ /* collection update actions */
public function deleteRows() public function deleteRows(Collection $coll)
{ {
//$collection->getDeleteDiff();
} }
public function updateRows() public function updateRows(Collection $coll)
{ {
} }
public function insertRows() public function insertRows(Collection $coll)
{
//$collection->getInsertDiff();
}
protected function _getDeleteRowSql()
{
}
protected function _getUpdateRowSql()
{
}
protected function _getDeleteRowSql()
{ {
} }
} }
?>
\ No newline at end of file
...@@ -31,14 +31,9 @@ namespace Doctrine\ORM\Persisters; ...@@ -31,14 +31,9 @@ namespace Doctrine\ORM\Persisters;
* @since 2.0 * @since 2.0
*/ */
abstract class AbstractEntityPersister abstract class AbstractEntityPersister
{ {
/** /**
* The names of all the fields that are available on entities. * Metadata object that describes the mapping of the mapped entity class.
*/
protected $_fieldNames = array();
/**
* Metadata object that descibes the mapping of the mapped entity class.
* *
* @var Doctrine\ORM\Mapping\ClassMetadata * @var Doctrine\ORM\Mapping\ClassMetadata
*/ */
...@@ -124,24 +119,12 @@ abstract class AbstractEntityPersister ...@@ -124,24 +119,12 @@ abstract class AbstractEntityPersister
/** /**
* *
* @return <type> * @return Doctrine\ORM\ClassMetadata
*/ */
public function getClassMetadata() public function getClassMetadata()
{ {
return $this->_classMetadata; return $this->_classMetadata;
} }
/**
* @todo Move to ClassMetadata?
*/
public function getFieldNames()
{
if ($this->_fieldNames) {
return $this->_fieldNames;
}
$this->_fieldNames = $this->_classMetadata->getFieldNames();
return $this->_fieldNames;
}
/** /**
* Gets the name of the class in the entity hierarchy that owns the field with * Gets the name of the class in the entity hierarchy that owns the field with
...@@ -156,15 +139,10 @@ abstract class AbstractEntityPersister ...@@ -156,15 +139,10 @@ abstract class AbstractEntityPersister
if ($this->_classMetadata->isInheritanceTypeNone()) { if ($this->_classMetadata->isInheritanceTypeNone()) {
return $this->_classMetadata; return $this->_classMetadata;
} else { } else {
foreach ($this->_classMetadata->getParentClasses() as $parentClass) { $mapping = $this->_classMetadata->getFieldMapping($fieldName);
$parentClassMetadata = Doctrine_ORM_Mapping_ClassMetadataFactory::getInstance() return $mapping['inherited'];
->getMetadataFor($parentClass);
if ( ! $parentClassMetadata->isInheritedField($fieldName)) {
return $parentClassMetadata;
}
}
} }
throw new Doctrine_Exception("Unable to find defining class of field '$fieldName'."); throw new DoctrineException("Unable to find defining class of field '$fieldName'.");
} }
/** /**
...@@ -186,8 +164,9 @@ abstract class AbstractEntityPersister ...@@ -186,8 +164,9 @@ abstract class AbstractEntityPersister
/** /**
* Prepares all the entity data for insertion into the database. * Prepares all the entity data for insertion into the database.
* *
* @param object $entity
* @param array $array * @param array $array
* @return void * @param boolean $isInsert
*/ */
protected function _prepareData($entity, array &$result, $isInsert = false) protected function _prepareData($entity, array &$result, $isInsert = false)
{ {
......
This diff is collapsed.
...@@ -27,7 +27,7 @@ class CmsUser ...@@ -27,7 +27,7 @@ class CmsUser
public $name; public $name;
/** /**
* @DoctrineOneToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsPhonenumber", * @DoctrineOneToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsPhonenumber",
mappedBy="user", cascade={"save"}) mappedBy="user", cascade={"save", "delete"})
*/ */
public $phonenumbers; public $phonenumbers;
/** /**
...@@ -44,4 +44,14 @@ class CmsUser ...@@ -44,4 +44,14 @@ class CmsUser
$this->phonenumbers[] = $phone; $this->phonenumbers[] = $phone;
$phone->user = $this; $phone->user = $this;
} }
public function removePhonenumber($index) {
if (isset($this->phonenumbers[$index])) {
$ph = $this->phonenumbers[$index];
unset($this->phonenumbers[$index]);
$ph->user = null;
return true;
}
return false;
}
} }
...@@ -42,24 +42,32 @@ class BasicCRUDTest extends \Doctrine\Tests\OrmFunctionalTestCase { ...@@ -42,24 +42,32 @@ class BasicCRUDTest extends \Doctrine\Tests\OrmFunctionalTestCase {
$em->flush(); $em->flush();
$this->assertTrue($em->contains($ph)); $this->assertTrue($em->contains($ph));
$this->assertTrue($em->contains($user)); $this->assertTrue($em->contains($user));
$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\Collection);
// Update // Update name
$user->name = 'guilherme'; $user->name = 'guilherme';
$em->flush(); $em->flush();
$this->assertEquals('guilherme', $user->name); $this->assertEquals('guilherme', $user->name);
// Add another phonenumber
$ph2 = new CmsPhonenumber;
$ph2->phonenumber = "6789";
$user->addPhonenumber($ph2);
$em->flush();
$this->assertTrue($em->contains($ph2));
// Delete // Delete
$em->delete($user); $em->delete($user);
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($user)); $this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($user));
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($ph));
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($ph2));
$em->flush(); $em->flush();
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($user)); $this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($user));
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($ph));
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($ph2));
} }
public function testMore() { /*public function testMore() {
#echo PHP_EOL . "SECOND" . PHP_EOL;
/*$user = new CmsUser;
$user->name = 'jon';
$user->*/
$ph = new CmsPhonenumber; $ph = new CmsPhonenumber;
$ph->phonenumber = 123456; $ph->phonenumber = 123456;
...@@ -67,6 +75,6 @@ class BasicCRUDTest extends \Doctrine\Tests\OrmFunctionalTestCase { ...@@ -67,6 +75,6 @@ class BasicCRUDTest extends \Doctrine\Tests\OrmFunctionalTestCase {
$this->_em->save($ph); $this->_em->save($ph);
$this->_em->flush(); $this->_em->flush();
} }*/
} }
...@@ -53,7 +53,6 @@ class ObjectHydratorTest extends HydrationTest ...@@ -53,7 +53,6 @@ class ObjectHydratorTest extends HydrationTest
$queryComponents, $tableAliasMap)); $queryComponents, $tableAliasMap));
$this->assertEquals(2, count($result)); $this->assertEquals(2, count($result));
$this->assertTrue($result instanceof \Doctrine\ORM\Collection);
$this->assertTrue($result[0] instanceof \Doctrine\Tests\Models\CMS\CmsUser); $this->assertTrue($result[0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
$this->assertTrue($result[1] instanceof \Doctrine\Tests\Models\CMS\CmsUser); $this->assertTrue($result[1] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
$this->assertEquals(1, $result[0]->id); $this->assertEquals(1, $result[0]->id);
...@@ -659,7 +658,6 @@ class ObjectHydratorTest extends HydrationTest ...@@ -659,7 +658,6 @@ class ObjectHydratorTest extends HydrationTest
$queryComponents, $tableAliasMap)); $queryComponents, $tableAliasMap));
$this->assertEquals(2, count($result)); $this->assertEquals(2, count($result));
$this->assertTrue($result instanceof \Doctrine\ORM\Collection);
$this->assertTrue($result[0] instanceof \Doctrine\Tests\Models\Forum\ForumCategory); $this->assertTrue($result[0] instanceof \Doctrine\Tests\Models\Forum\ForumCategory);
$this->assertTrue($result[1] instanceof \Doctrine\Tests\Models\Forum\ForumCategory); $this->assertTrue($result[1] instanceof \Doctrine\Tests\Models\Forum\ForumCategory);
$this->assertEquals(1, $result[0]->getId()); $this->assertEquals(1, $result[0]->getId());
......
...@@ -159,7 +159,7 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase ...@@ -159,7 +159,7 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase
)); ));
// Go // Go
$this->_unitOfWork->computeEntityChangeSets(array($user1, $user2)); $this->_unitOfWork->computeChangeSets(array($user1, $user2));
// Verify // Verify
$user1ChangeSet = $this->_unitOfWork->getEntityChangeSet($user1); $user1ChangeSet = $this->_unitOfWork->getEntityChangeSet($user1);
......
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