Permalink
Browse files

Merge dominikl/DDC-1278 into doctrine/DDC-1278

  • Loading branch information...
beberlei committed Oct 16, 2011
2 parents fc73845 + 6e47d7b commit 22a04fd6de3ffa44cc3cd817d65cf112b1df7b53
@@ -422,16 +422,11 @@ public function getPartialReference($entityName, $identifier)
* Clears the EntityManager. All entities that are currently managed
* by this EntityManager become detached.
*
- * @param string $entityName
+ * @param string $entityName if given, only entities of this type will get detached
*/
public function clear($entityName = null)
{
- if ($entityName === null) {
- $this->unitOfWork->clear();
- } else {
- //TODO
- throw new ORMException("EntityManager#clear(\$entityName) not yet implemented.");
- }
+ $this->unitOfWork->clear($entityName);
}
/**
@@ -1567,8 +1567,9 @@ public function detach($entity)
*
* @param object $entity
* @param array $visited
+ * @param boolean $noCascade if true, don't cascade detach operation
*/
- private function doDetach($entity, array &$visited)
+ private function doDetach($entity, array &$visited, $noCascade = false)
{
$oid = spl_object_hash($entity);
if (isset($visited[$oid])) {
@@ -1590,8 +1591,10 @@ private function doDetach($entity, array &$visited)
case self::STATE_DETACHED:
return;
}
-
- $this->cascadeDetach($entity, $visited);
+
+ if (!$noCascade) {
+ $this->cascadeDetach($entity, $visited);
+ }
}
/**
@@ -1842,24 +1845,37 @@ public function getCommitOrderCalculator()
/**
* Clears the UnitOfWork.
- */
- public function clear()
- {
- $this->identityMap =
- $this->entityIdentifiers =
- $this->originalEntityData =
- $this->entityChangeSets =
- $this->entityStates =
- $this->scheduledForDirtyCheck =
- $this->entityInsertions =
- $this->entityUpdates =
- $this->entityDeletions =
- $this->collectionDeletions =
- $this->collectionUpdates =
- $this->extraUpdates =
- $this->orphanRemovals = array();
- if ($this->commitOrderCalculator !== null) {
- $this->commitOrderCalculator->clear();
+ *
+ * @param string $entityName if given, only entities of this type will get detached
+ */
+ public function clear($entityName = null)
+ {
+ if ($entityName === null) {
+ $this->identityMap =
+ $this->entityIdentifiers =
+ $this->originalEntityData =
+ $this->entityChangeSets =
+ $this->entityStates =
+ $this->scheduledForDirtyCheck =
+ $this->entityInsertions =
+ $this->entityUpdates =
+ $this->entityDeletions =
+ $this->collectionDeletions =
+ $this->collectionUpdates =
+ $this->extraUpdates =
+ $this->orphanRemovals = array();
+ if ($this->commitOrderCalculator !== null) {
+ $this->commitOrderCalculator->clear();
+ }
+ } else {
+ $visited = array();
+ foreach ($this->identityMap as $className => $entities) {
+ if ($className === $entityName) {
+ foreach ($entities as $entity) {
+ $this->doDetach($entity, $visited, true);
+ }
+ }
+ }
}
if ($this->evm->hasListeners(Events::onClear)) {
@@ -35,7 +35,7 @@ class CmsUser
*/
public $phonenumbers;
/**
- * @OneToMany(targetEntity="CmsArticle", mappedBy="user")
+ * @OneToMany(targetEntity="CmsArticle", mappedBy="user", cascade={"detach"})
*/
public $articles;
/**
@@ -48,7 +48,7 @@ class CmsUser
*/
public $email;
/**
- * @ManyToMany(targetEntity="CmsGroup", inversedBy="users", cascade={"persist", "merge"})
+ * @ManyToMany(targetEntity="CmsGroup", inversedBy="users", cascade={"persist", "merge", "detach"})
* @JoinTable(name="cms_users_groups",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
@@ -980,4 +980,54 @@ public function testManyToOneFetchModeQuery()
$this->assertTrue($article->user->__isInitialized__, "...but its initialized!");
$this->assertEquals($qc+2, $this->getCurrentQueryCount());
}
+
+ /**
+ * @group DDC-1278
+ */
+ public function testClearWithEntityName()
+ {
+ $user = new CmsUser;
+ $user->name = 'Dominik';
+ $user->username = 'domnikl';
+ $user->status = 'developer';
+
+ $address = new CmsAddress();
+ $address->city = "Springfield";
+ $address->zip = "12354";
+ $address->country = "Germany";
+ $address->street = "Foo Street";
+ $address->user = $user;
+ $user->address = $address;
+
+ $article1 = new CmsArticle();
+ $article1->topic = 'Foo';
+ $article1->text = 'Foo Text';
+
+ $article2 = new CmsArticle();
+ $article2->topic = 'Bar';
+ $article2->text = 'Bar Text';
+
+ $user->addArticle($article1);
+ $user->addArticle($article2);
+
+ $this->_em->persist($article1);
+ $this->_em->persist($article2);
+ $this->_em->persist($address);
+ $this->_em->persist($user);
+ $this->_em->flush();
+
+ $unitOfWork = $this->_em->getUnitOfWork();
+
+ $this->_em->clear('Doctrine\Tests\Models\CMS\CmsUser');
+
+ $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($user));
+
+ $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $unitOfWork->getEntityState($address));
+ $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $unitOfWork->getEntityState($article1));
+ $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $unitOfWork->getEntityState($article2));
+
+ $this->_em->clear();
+
+ $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($address));
+ }
}

0 comments on commit 22a04fd

Please sign in to comment.