Browse Source

Merge pull request #301 from mixegen/soft-delete

[SoftDeleteableFilter] Small changes in filter.
Gustavo Falco 13 years ago
parent
commit
2e150dff97

+ 24 - 17
lib/Gedmo/SoftDeleteable/Filter/SoftDeleteableFilter.php

@@ -20,47 +20,54 @@ use Doctrine\ORM\Mapping\ClassMetaData,
 
 class SoftDeleteableFilter extends SQLFilter
 {
-    protected $configuration;
-
+    protected $listener;
+    protected $entityManager;
 
     public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
     {
-        $config = $this->getConfiguration($targetEntity);
+        $config = $this->getListener()->getConfiguration($this->getEntityManager(), $targetEntity->name);
 
         if (!isset($config['softDeleteable']) || !$config['softDeleteable']) {
             return '';
         }
 
-        return $targetTableAlias.'.'.$config['fieldName'].' IS NULL';
+        $column = $targetEntity->columnNames[$config['fieldName']];
+
+        return $targetTableAlias.'.'.$column.' IS NULL';
     }
 
-    protected function getConfiguration(ClassMetadata $meta)
+    protected function getListener()
     {
-        if ($this->configuration === null) {
-            $refl = new \ReflectionProperty('Doctrine\ORM\Query\Filter\SQLFilter', 'em');
-            $refl->setAccessible(true);
-            $em = $refl->getValue($this);
+        if ($this->listener === null) {
+            $em = $this->getEntityManager();
             $evm = $em->getEventManager();
 
             foreach ($evm->getListeners() as $listeners) {
                 foreach ($listeners as $listener) {
                     if ($listener instanceof SoftDeleteableListener) {
-                        $this->configuration = $listener->getConfiguration($em, $meta->name);
+                        $this->listener = $listener;
 
-                        break;
+                        break 2;
                     }
                 }
-
-                if ($this->configuration === null) {
-                    break;
-                }
             }
 
-            if ($this->configuration === null) {
+            if (!$this->listener === null) {
                 throw new \RuntimeException('Listener "SoftDeleteableListener" was not added to the EventManager!');
             }
         }
 
-        return $this->configuration;
+        return $this->listener;
+    }
+
+    protected function getEntityManager()
+    {
+        if ($this->entityManager === null) {
+            $refl = new \ReflectionProperty('Doctrine\ORM\Query\Filter\SQLFilter', 'em');
+            $refl->setAccessible(true);
+            $this->entityManager = $refl->getValue($this);
+        }
+
+        return $this->entityManager;
     }
 }

+ 54 - 0
tests/Gedmo/SoftDeleteable/Fixture/Entity/User.php

@@ -0,0 +1,54 @@
+<?php
+namespace SoftDeleteable\Fixture\Entity;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ * @Gedmo\SoftDeleteable(fieldName="deletedAt")
+ */
+class User
+{
+    /**
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="IDENTITY")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(name="title", type="string")
+     */
+    private $username;
+
+    /**
+     * @ORM\Column(name="deleted_time", type="datetime", nullable=true)
+     */
+    private $deletedAt;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setUsername($username)
+    {
+        $this->username = $username;
+    }
+
+    public function getUsername()
+    {
+        return $this->username;
+    }
+
+    public function setDeletedAt($deletedAt)
+    {
+        $this->deletedAt = $deletedAt;
+    }
+
+    public function getDeletedAt()
+    {
+        return $this->deletedAt;
+    }
+}

+ 29 - 1
tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php

@@ -7,6 +7,7 @@ use Doctrine\Common\EventManager;
 use Doctrine\Common\Util\Debug,
     SoftDeleteable\Fixture\Entity\Article,
     SoftDeleteable\Fixture\Entity\Comment,
+    SoftDeleteable\Fixture\Entity\User,
     SoftDeleteable\Fixture\Entity\Page,
     SoftDeleteable\Fixture\Entity\MegaPage,
     SoftDeleteable\Fixture\Entity\Module,
@@ -29,6 +30,7 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
     const MEGA_PAGE_CLASS = 'SoftDeleteable\Fixture\Entity\MegaPage';
     const MODULE_CLASS = 'SoftDeleteable\Fixture\Entity\Module';
     const SOFT_DELETEABLE_FILTER_NAME = 'soft-deleteable';
+    const USER_CLASS = 'SoftDeleteable\Fixture\Entity\User';
 
     private $softDeleteableListener;
 
@@ -45,6 +47,31 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
         $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
     }
 
+    /**
+     * @test
+     */
+    public function shouldSoftlyDeleteIfColumnNameDifferFromPropertyName()
+    {
+        $repo = $this->em->getRepository(self::USER_CLASS);
+
+        $newUser = new User();
+        $username = 'test_user';
+        $newUser->setUsername($username);
+
+        $this->em->persist($newUser);
+        $this->em->flush();
+
+        $user = $repo->findOneBy(array('username' => $username));
+
+        $this->assertNull($user->getDeletedAt());
+
+        $this->em->remove($user);
+        $this->em->flush();
+
+        $user = $repo->findOneBy(array('username' => $username));
+        $this->assertNull($user);
+    }
+
     public function testSoftDeleteable()
     {
         $repo = $this->em->getRepository(self::ARTICLE_CLASS);
@@ -168,7 +195,8 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
             self::PAGE_CLASS,
             self::MEGA_PAGE_CLASS,
             self::MODULE_CLASS,
-            self::COMMENT_CLASS
+            self::COMMENT_CLASS,
+            self::USER_CLASS
         );
     }