Commit 36763dad authored by romanb's avatar romanb

A little progress on the UnitOfWork.

parent 0ac97e7a
......@@ -9,6 +9,7 @@ namespace Doctrine\Common\Collections;
use \Countable;
use \IteratorAggregate;
use \ArrayAccess;
use \ArrayIterator;
/**
* 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
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()
*/
......@@ -174,7 +191,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
* Tests for the existance of an element that satisfies the given predicate.
*
* @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) {
foreach ($this->_data as $key => $element)
......@@ -191,7 +208,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
*/
public function containsAll($otherColl)
{
throw new Doctrine_Exception("Not yet implemented.");
throw new DoctrineException("Not yet implemented.");
}
/**
......
......@@ -495,7 +495,7 @@ class Connection
*/
public function prepare($statement)
{
echo $statement;
echo $statement . PHP_EOL;
$this->connect();
try {
return $this->_conn->prepare($statement);
......@@ -561,6 +561,7 @@ class Connection
$this->connect();
try {
if ( ! empty($params)) {
//var_dump($params);
$stmt = $this->prepare($query);
$stmt->execute($params);
return $stmt->rowCount();
......
......@@ -9,6 +9,7 @@
* most method calls to the EntityManager.
*
* @since 2.0
* @todo Any takers for this one? Needs a rewrite.
*/
class Doctrine_ORM_ActiveEntity
{
......
......@@ -21,11 +21,13 @@
namespace Doctrine\ORM;
use Doctrine\ORM\Mapping\AssociationMapping;
/**
* A persistent collection wrapper.
*
* A PersistentCollection represents a collection of entities. Collections of
* entities represent only the associations (links) to those entities.
* A PersistentCollection represents a collection of elements that have persistent state.
* 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
* 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
......@@ -104,13 +106,17 @@ final class Collection extends \Doctrine\Common\Collections\Collection
*/
private $_hydrationFlag;
/**
* The class descriptor of the owning entity.
*/
private $_ownerClass;
/**
* 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->_em = $em;
$this->_ownerClass = $em->getClassMetadata($entityBaseType);
......@@ -151,7 +157,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection
* @param object $entity
* @param AssociationMapping $relation
*/
public function _setOwner($entity, \Doctrine\ORM\Mapping\AssociationMapping $relation)
public function _setOwner($entity, AssociationMapping $relation)
{
$this->_owner = $entity;
$this->_association = $relation;
......@@ -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
* @return boolean
......@@ -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 string $key
* @return boolean
* @return TRUE
* @override
*/
public function add($value)
{
$result = parent::add($value);
if ( ! $result) return $result; // EARLY EXIT
if ($this->_hydrationFlag) {
if ($this->_backRefFieldName) {
// set back reference to owner
$this->_ownerClass->getReflectionProperty(
$this->_backRefFieldName)->setValue($value, $this->_owner);
$this->_ownerClass->getReflectionProperty($this->_backRefFieldName)
->setValue($value, $this->_owner);
}
} else {
//TODO: Register collection as dirty with the UoW if necessary
......@@ -295,27 +301,6 @@ final class Collection extends \Doctrine\Common\Collections\Collection
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:
* getDeleteDiff
......@@ -324,7 +309,7 @@ final class Collection extends \Doctrine\Common\Collections\Collection
*/
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
*/
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
private function _changed()
{
/*if ( ! $this->_em->getUnitOfWork()->isCollectionScheduledForUpdate($this)) {
$this->_em->getUnitOfWork()->scheduleCollectionUpdate($this);
}*/
if ( ! $this->_em->getUnitOfWork()->isCollectionScheduledForUpdate($this)) {
//var_dump(get_class($this->_snapshot[0]));
//echo "NOT!";
//$this->_em->getUnitOfWork()->scheduleCollectionUpdate($this);
}
}
}
......@@ -54,7 +54,7 @@ class ObjectHydrator extends AbstractHydrator
if ($this->_parserResult->isMixedQuery()) {
$result = array();
} else {
$result = new \Doctrine\ORM\Collection($this->_em, $this->_rootEntityName);
$result = new \Doctrine\Common\Collections\Collection;
}
$cache = array();
......@@ -66,7 +66,7 @@ class ObjectHydrator extends AbstractHydrator
foreach ($this->_collections as $coll) {
$coll->_takeSnapshot();
$coll->_setHydrationFlag(false);
$this->_uow->addManagedCollection($coll);
//$this->_uow->addManagedCollection($coll);
}
// Clean up
......@@ -105,7 +105,7 @@ class ObjectHydrator extends AbstractHydrator
if ( ! is_object($coll)) {
end($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) {
$this->_resultPointers[$dqlAlias] = $coll->last();
}
......
<?php
class Doctrine_ORM_Persisters_AbstractCollectionPersister
namespace Doctrine\ORM\Persisters;
use Doctrine\ORM\Collection;
class AbstractCollectionPersister
{
public function recreate(Doctrine_Collection $coll)
......@@ -8,7 +12,6 @@ class Doctrine_ORM_Persisters_AbstractCollectionPersister
if ($coll->getRelation()->isInverseSide()) {
return;
}
//...
}
......@@ -17,32 +20,39 @@ class Doctrine_ORM_Persisters_AbstractCollectionPersister
if ($coll->getRelation()->isInverseSide()) {
return;
}
//...
if ($coll->getRelation() instanceof Doctrine_Association_OneToManyMapping) {
//...
} else if ($coll->getRelation() instanceof Doctrine_Association_ManyToManyMapping) {
//...
}
}
/* 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;
* @since 2.0
*/
abstract class AbstractEntityPersister
{
{
/**
* The names of all the fields that are available on entities.
*/
protected $_fieldNames = array();
/**
* Metadata object that descibes the mapping of the mapped entity class.
* Metadata object that describes the mapping of the mapped entity class.
*
* @var Doctrine\ORM\Mapping\ClassMetadata
*/
......@@ -124,24 +119,12 @@ abstract class AbstractEntityPersister
/**
*
* @return <type>
* @return Doctrine\ORM\ClassMetadata
*/
public function getClassMetadata()
{
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
......@@ -156,15 +139,10 @@ abstract class AbstractEntityPersister
if ($this->_classMetadata->isInheritanceTypeNone()) {
return $this->_classMetadata;
} else {
foreach ($this->_classMetadata->getParentClasses() as $parentClass) {
$parentClassMetadata = Doctrine_ORM_Mapping_ClassMetadataFactory::getInstance()
->getMetadataFor($parentClass);
if ( ! $parentClassMetadata->isInheritedField($fieldName)) {
return $parentClassMetadata;
}
}
$mapping = $this->_classMetadata->getFieldMapping($fieldName);
return $mapping['inherited'];
}
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
/**
* Prepares all the entity data for insertion into the database.
*
* @param object $entity
* @param array $array
* @return void
* @param boolean $isInsert
*/
protected function _prepareData($entity, array &$result, $isInsert = false)
{
......
This diff is collapsed.
......@@ -27,7 +27,7 @@ class CmsUser
public $name;
/**
* @DoctrineOneToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsPhonenumber",
mappedBy="user", cascade={"save"})
mappedBy="user", cascade={"save", "delete"})
*/
public $phonenumbers;
/**
......@@ -44,4 +44,14 @@ class CmsUser
$this->phonenumbers[] = $phone;
$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 {
$em->flush();
$this->assertTrue($em->contains($ph));
$this->assertTrue($em->contains($user));
$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\Collection);
// Update
// Update name
$user->name = 'guilherme';
$em->flush();
$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
$em->delete($user);
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($user));
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($ph));
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($ph2));
$em->flush();
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($user));
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($ph));
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($ph2));
}
public function testMore() {
#echo PHP_EOL . "SECOND" . PHP_EOL;
/*$user = new CmsUser;
$user->name = 'jon';
$user->*/
/*public function testMore() {
$ph = new CmsPhonenumber;
$ph->phonenumber = 123456;
......@@ -67,6 +75,6 @@ class BasicCRUDTest extends \Doctrine\Tests\OrmFunctionalTestCase {
$this->_em->save($ph);
$this->_em->flush();
}
}*/
}
......@@ -53,7 +53,6 @@ class ObjectHydratorTest extends HydrationTest
$queryComponents, $tableAliasMap));
$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[1] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
$this->assertEquals(1, $result[0]->id);
......@@ -659,7 +658,6 @@ class ObjectHydratorTest extends HydrationTest
$queryComponents, $tableAliasMap));
$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[1] instanceof \Doctrine\Tests\Models\Forum\ForumCategory);
$this->assertEquals(1, $result[0]->getId());
......
......@@ -159,7 +159,7 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase
));
// Go
$this->_unitOfWork->computeEntityChangeSets(array($user1, $user2));
$this->_unitOfWork->computeChangeSets(array($user1, $user2));
// Verify
$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