Преглед изворни кода

fixed protected property mapping issue on inherited classes

gediminasm пре 14 година
родитељ
комит
efa40ac792

+ 6 - 0
lib/Gedmo/Sluggable/SluggableListener.php

@@ -398,6 +398,12 @@ class SluggableListener implements EventSubscriber
         $class = $meta->getReflectionClass();
         // property annotations
         foreach ($class->getProperties() as $property) {
+            if ($meta->isMappedSuperclass && !$property->isPrivate() ||
+                $meta->isInheritedField($property->name) ||
+                $meta->isInheritedAssociation($property->name)
+            ) {
+                continue;
+            }
             // sluggable property
             if ($sluggable = $reader->getPropertyAnnotation($property, self::ANNOTATION_SLUGGABLE)) {
                 $field = $property->getName();

+ 7 - 1
lib/Gedmo/Timestampable/TimestampableListener.php

@@ -249,7 +249,13 @@ class TimestampableListener implements EventSubscriber
     
         $class = $meta->getReflectionClass();
         // property annotations
-        foreach ($class->getProperties() as $property) {            
+        foreach ($class->getProperties() as $property) {
+            if ($meta->isMappedSuperclass && !$property->isPrivate() ||
+                $meta->isInheritedField($property->name) ||
+                $meta->isInheritedAssociation($property->name)
+            ) {
+                continue;
+            }
             if ($timestampable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TIMESTAMPABLE)) {
                 $field = $property->getName();
                 if (!$meta->hasField($field)) {

+ 4 - 1
lib/Gedmo/Translatable/TranslationListener.php

@@ -643,7 +643,10 @@ class TranslationListener implements EventSubscriber
         
         // property annotations
         foreach ($class->getProperties() as $property) {
-            if ($meta->isInheritedField($property->name) || $meta->isInheritedAssociation($property->name)) {
+            if ($meta->isMappedSuperclass && !$property->isPrivate() ||
+                $meta->isInheritedField($property->name) ||
+                $meta->isInheritedAssociation($property->name)
+            ) {
                 continue;
             }
             // translatable property

+ 6 - 0
lib/Gedmo/Tree/TreeListener.php

@@ -343,6 +343,12 @@ class TreeListener implements EventSubscriber
         $class = $meta->getReflectionClass();
         // property annotations
         foreach ($class->getProperties() as $property) {
+            if ($meta->isMappedSuperclass && !$property->isPrivate() ||
+                $meta->isInheritedField($property->name) ||
+                $meta->isInheritedAssociation($property->name)
+            ) {
+                continue;
+            }
             // left
             if ($left = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEFT)) {
                 $field = $property->getName();

+ 82 - 0
tests/Gedmo/Timestampable/Fixture/MappedSupperClass.php

@@ -0,0 +1,82 @@
+<?php
+
+namespace Timestampable\Fixture;
+
+/**
+* @MappedSuperclass
+*/
+class MappedSupperClass
+{
+    /**
+    * @var integer $id
+    *
+    * @Column(name="id", type="integer")
+    * @Id
+    * @GeneratedValue(strategy="AUTO")
+    */
+    protected $id;
+    
+    /**
+    * @var string $locale
+    *
+    * @gedmo:Locale
+    */
+    protected $locale;
+    
+    /**
+    * @var string $title
+    *
+    * @gedmo:Translatable
+    * @Column(name="name", type="string", length=255)
+    */
+    protected $name;
+    
+    /**
+    * @var \DateTime $createdAt
+    *
+    * @Column(name="created_at", type="datetime")
+    * @gedmo:Timestampable(on="create")
+    */
+    protected $createdAt;
+    
+    /**
+    * Get id
+    *
+    * @return integer $id
+    * @codeCoverageIgnore
+    */
+    public function getId()
+    {
+        return $this->id;
+    }
+    
+    /**
+    * Set name
+    *
+    * @param string $name
+    */
+    public function setName($name)
+    {
+        $this->name = $name;
+    }
+
+    /**
+    * Get name
+    *
+    * @return string $name
+    */
+    public function getName()
+    {
+        return $this->name;
+    }
+    
+    /**
+    * Get createdAt
+    *
+    * @return \DateTime $createdAt
+    */
+    public function getCreatedAt()
+    {
+        return $this->createdAt;
+    }
+}

+ 25 - 0
tests/Gedmo/Timestampable/Fixture/SupperClassExtension.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Timestampable\Fixture;
+
+/**
+ * @Entity
+ */
+class SupperClassExtension extends MappedSupperClass
+{
+    /**
+     * @Column(length=128)
+     * @gedmo:Translatable
+     */
+    private $title;
+    
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+}

+ 73 - 0
tests/Gedmo/Timestampable/ProtectedPropertySupperclassTest.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace Gedmo\Tree;
+
+use Doctrine\Common\Util\Debug;
+
+/**
+ * These are tests for Timestampable behavior
+ * 
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Tree
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class ProtectedPropertySupperclassTest extends \PHPUnit_Framework_TestCase
+{
+    const TEST_ENTITY_CLASS = "Timestampable\Fixture\SupperClassExtension";
+    const TEST_ENTITY_TRANSLATION = "Gedmo\Translatable\Entity\Translation";
+    private $em;
+
+    public function setUp()
+    {
+        $classLoader = new \Doctrine\Common\ClassLoader('Timestampable\Fixture', __DIR__ . '/../');
+        $classLoader->register();
+        
+        $config = new \Doctrine\ORM\Configuration();
+        $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
+        $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
+        $config->setProxyDir(__DIR__ . '/Proxy');
+        $config->setProxyNamespace('Gedmo\Timestampable\Proxies');
+        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
+
+        $conn = array(
+            'driver' => 'pdo_sqlite',
+            'memory' => true,
+        );
+
+        //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
+        
+        $evm = new \Doctrine\Common\EventManager();
+        $evm->addEventSubscriber(new \Gedmo\Timestampable\TimestampableListener());
+        $translationListener = new \Gedmo\Translatable\TranslationListener();
+        $translationListener->setTranslatableLocale('en_us');
+        $evm->addEventSubscriber($translationListener);
+        $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
+
+        $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
+        $schemaTool->dropSchema(array());
+        $schemaTool->createSchema(array(
+            $this->em->getClassMetadata(self::TEST_ENTITY_CLASS),
+            $this->em->getClassMetadata(self::TEST_ENTITY_TRANSLATION)
+        ));
+    }
+    
+    public function testProtectedProperty()
+    {
+        $test = new \Timestampable\Fixture\SupperClassExtension;
+        $test->setName('name');
+        $test->setTitle('title');
+        
+        $this->em->persist($test);
+        $this->em->flush();
+        $this->em->clear();
+        
+        $repo = $this->em->getRepository(self::TEST_ENTITY_TRANSLATION);
+        $translations = $repo->findTranslations($test);
+        $this->assertEquals(1, count($translations));
+        $this->assertArrayHasKey('en_us', $translations);
+        $this->assertEquals(2, count($translations['en_us']));
+        
+        $this->assertTrue($test->getCreatedAt() !== null);
+    }
+}