Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

DDC-1278 - EntityManager::clear($entity) support #105

Merged
merged 4 commits into from

2 participants

@domnikl

added cascade detach operation only for entites with the same name when $entityName on clear() was specified. I am still unsure where to put my tests for that.

Dominik Liebler added some commits
Dominik Liebler DDC-1278 - EntityManager::clear($entity) support
added new parameter $entityName for UnitOfWork::clear()
removed not implemented exception in EntityManager:clear()
05fb0b9
Dominik Liebler fixed typo 745535d
Dominik Liebler DDC-1278 - EntityManager::clear($entity) support
cascade detach operation only on entity name entities
25f5ff0
lib/Doctrine/ORM/UnitOfWork.php
((5 lines not shown))
*/
- private function doDetach($entity, array &$visited)
+ private function doDetach($entity, array &$visited, $entityName = null)
@beberlei Owner

i think you can use a boolean flag here for "noCascade = true/false".

Reason is simle, in clear() you already have all entities of $entityName, you dont need to detach them twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@beberlei
Owner

Still no tests on this one, please add tests. You can run the testsuite by calling "phpunit" from the root directory of Doctrine. You can put a test for this into Doctrine\Tests\ORM\Functional\ into the BasicSomethingTest

Dominik Liebler DDC-1278 - EntityManager::clear($entity) support
added test case and modified test data CmsUser to cascade detach address and articles (testing collections and single entites)
6e47d7b
@domnikl

Ok, I changed it to a boolean flag and finally added a test for this.

@beberlei beberlei merged commit 6e47d7b into doctrine:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 11, 2011
  1. DDC-1278 - EntityManager::clear($entity) support

    Dominik Liebler authored
    added new parameter $entityName for UnitOfWork::clear()
    removed not implemented exception in EntityManager:clear()
Commits on Aug 12, 2011
  1. fixed typo

    Dominik Liebler authored
Commits on Aug 13, 2011
  1. DDC-1278 - EntityManager::clear($entity) support

    Dominik Liebler authored
    cascade detach operation only on entity name entities
Commits on Aug 14, 2011
  1. DDC-1278 - EntityManager::clear($entity) support

    Dominik Liebler authored
    added test case and modified test data CmsUser to cascade detach address and articles (testing collections and single entites)
This page is out of date. Refresh to see the latest.
View
9 lib/Doctrine/ORM/EntityManager.php
@@ -421,16 +421,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);
}
/**
View
58 lib/Doctrine/ORM/UnitOfWork.php
@@ -1515,8 +1515,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])) {
@@ -1538,8 +1539,10 @@ private function doDetach($entity, array &$visited)
case self::STATE_DETACHED:
return;
}
-
- $this->cascadeDetach($entity, $visited);
+
+ if (!$noCascade) {
+ $this->cascadeDetach($entity, $visited);
+ }
}
/**
@@ -1790,24 +1793,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)) {
View
4 tests/Doctrine/Tests/Models/CMS/CmsUser.php
@@ -35,7 +35,7 @@ class CmsUser
*/
public $phonenumbers;
/**
- * @OneToMany(targetEntity="CmsArticle", mappedBy="user")
+ * @OneToMany(targetEntity="CmsArticle", mappedBy="user", cascade={"detach"})
*/
public $articles;
/**
@@ -43,7 +43,7 @@ class CmsUser
*/
public $address;
/**
- * @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")}
View
50 tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php
@@ -981,4 +981,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));
+ }
}
Something went wrong with that request. Please try again.