Forráskód Böngészése

[sluggable] unique slug support for odm listener

gediminasm 14 éve
szülő
commit
8c43a1049a

+ 3 - 1
.gitignore

@@ -1,3 +1,5 @@
 tests/phpunit.xml
+tests/Gedmo/*/Proxy/*
+tests/Gedmo/*/Hydrator/*
 build/
-scripts/
+scripts/

+ 2 - 1
lib/Gedmo/Sluggable/AbstractSluggableListener.php

@@ -11,6 +11,7 @@ use Doctrine\Common\EventArgs,
  * object managers.
  * 
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @author Klein Florian <florian.klein@free.fr>
  * @subpackage AbstractSluggableListener
  * @package Gedmo.Sluggable
  * @link http://www.gediminasm.org
@@ -246,7 +247,7 @@ abstract class AbstractSluggableListener extends MappedEventSubscriber
 
             $mapping = $meta->getFieldMapping($config['slug']);
             $needRecursion = false;
-            if (strlen($generatedSlug) > $mapping['length']) {
+            if (isset($mapping['length']) && strlen($generatedSlug) > $mapping['length']) {
                 $needRecursion = true;
                 $generatedSlug = substr(
                     $generatedSlug, 

+ 16 - 1
lib/Gedmo/Sluggable/ODM/MongoDB/SluggableListener.php

@@ -4,6 +4,7 @@ namespace Gedmo\Sluggable\ODM\MongoDB;
 
 use Doctrine\ODM\MongoDB\Events,
     Doctrine\Common\EventArgs,
+    Doctrine\ODM\MongoDB\Cursor,
     Gedmo\Sluggable\AbstractSluggableListener;
 
 /**
@@ -14,6 +15,7 @@ use Doctrine\ODM\MongoDB\Events,
  * since it does some additional calculations on persisted documents.
  * 
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @author Klein Florian <florian.klein@free.fr>
  * @subpackage SluggableListener
  * @package Gedmo.Sluggable.ODM.MongoDB
  * @link http://www.gediminasm.org
@@ -80,6 +82,19 @@ class SluggableListener extends AbstractSluggableListener
      */
     protected function getUniqueSlugResult($om, $object, $meta, array $config, $preferedSlug)
     {
-        return array();
+        $qb = $om->createQueryBuilder($meta->name);
+        $identifier = $meta->getIdentifierValue($object);
+        $q = $qb->field($meta->identifier)->notEqual($identifier)
+            ->where("function() {
+            	return this.{$config['slug']}.indexOf('{$preferedSlug}') == 0;
+        	}")->getQuery();
+        $q->setHydrate(false);
+        
+        $result = $q->execute();
+        if ($result instanceof Cursor) {
+            $result = $result->toArray();
+        }
+        
+        return $result;
     }
 }

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

@@ -14,6 +14,7 @@ use Doctrine\ORM\Events,
  * since it does some additional calculations on persisted entities.
  * 
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @author Klein Florian <florian.klein@free.fr>
  * @subpackage SluggableListener
  * @package Gedmo.Sluggable
  * @link http://www.gediminasm.org

+ 41 - 13
tests/Gedmo/Sluggable/SluggableDocumentTest.php

@@ -51,27 +51,55 @@ class SluggableDocumentTest extends \PHPUnit_Framework_TestCase
             $evm
         );
         
+        $this->populate();
     }
     
-    public function testSluggable()
+    public function testSlugGeneration()
     {
-        $art0 = new Article();
-        $art0->setTitle('hello');
-        $art0->setCode('code');
+        // test insert
+        $repo = $this->dm->getRepository(self::TEST_ENTITY_CLASS);
+        $article = $repo->findOneByTitle('My Title');
         
-        $this->dm->persist($art0);
+        $this->assertEquals('my-title-the-code', $article->getSlug());
+        
+        // test update
+        $article->setTitle('New Title');
+        
+        $this->dm->persist($article);
         $this->dm->flush();
+        $this->dm->clear();
         
-        $this->assertEquals('hello-code', $art0->getSlug());
+        $article = $repo->findOneByTitle('New Title');
+        $this->assertEquals('new-title-the-code', $article->getSlug());
+    }
+    
+    public function testUniqueSlugGeneration()
+    {
+        for ($i = 0; $i < 12; $i++) {
+            $article = new Article();
+            $article->setTitle('My Title');
+            $article->setCode('The Code');
+            
+            $this->dm->persist($article);
+            $this->dm->flush();
+            $this->dm->clear();
+            $this->assertEquals($article->getSlug(), 'my-title-the-code-' . ($i + 1));
+        }
+    }
+    
+    private function populate()
+    {
+        $qb = $this->dm->createQueryBuilder(self::TEST_ENTITY_CLASS);
+        $q = $qb->remove()
+            ->getQuery();
+        $q->execute();
         
-        $repo = $this->dm->getRepository(self::TEST_ENTITY_CLASS);
-        $art = $repo->findOneByTitle('hello');
-        $art->setTitle('New Title');
+        $art0 = new Article();
+        $art0->setTitle('My Title');
+        $art0->setCode('The Code');
         
-        $this->dm->persist($art);
+        $this->dm->persist($art0);
         $this->dm->flush();
-        
-        $art = $repo->findOneByTitle('New Title');
-        $this->assertEquals('new-title-code', $art->getSlug());
+        $this->dm->clear();
     }
 }

+ 13 - 1
tests/Gedmo/Timestampable/Proxy/TimestampableFixtureArticleProxy.php

@@ -27,6 +27,12 @@ class TimestampableFixtureArticleProxy extends \Timestampable\Fixture\Article im
     }
 
     
+    public function setType($type)
+    {
+        $this->_load();
+        return parent::setType($type);
+    }
+
     public function getId()
     {
         $this->_load();
@@ -63,6 +69,12 @@ class TimestampableFixtureArticleProxy extends \Timestampable\Fixture\Article im
         return parent::getCreated();
     }
 
+    public function getPublished()
+    {
+        $this->_load();
+        return parent::getPublished();
+    }
+
     public function getUpdated()
     {
         $this->_load();
@@ -72,6 +84,6 @@ class TimestampableFixtureArticleProxy extends \Timestampable\Fixture\Article im
 
     public function __sleep()
     {
-        return array('__isInitialized__', 'id', 'title', 'comments', 'created', 'updated');
+        return array('__isInitialized__', 'id', 'title', 'comments', 'created', 'updated', 'published', 'type');
     }
 }

+ 1 - 1
tests/Gedmo/Timestampable/Proxy/TimestampableFixtureTypeProxy.php

@@ -48,6 +48,6 @@ class TimestampableFixtureTypeProxy extends \Timestampable\Fixture\Type implemen
 
     public function __sleep()
     {
-        return array('__isInitialized__', 'id', 'title', 'comments');
+        return array('__isInitialized__', 'id', 'title', 'articles');
     }
 }

+ 0 - 11
tests/Gedmo/Translatable/Proxy/Test.php

@@ -1,11 +0,0 @@
-<?php
-
-namespace Gedmo\Translatable\Proxies;
-
-class Test
-{
-    /**
-     * @gedmo:Translatable
-     */
-    protected $omg;
-}

+ 1 - 1
tests/Gedmo/Tree/Proxy/TreeFixtureCategoryProxy.php

@@ -60,6 +60,6 @@ class TreeFixtureCategoryProxy extends \Tree\Fixture\Category implements \Doctri
 
     public function __sleep()
     {
-        return array('__isInitialized__', 'id', 'title', 'lft', 'rgt', 'parent', 'children', 'comments');
+        return array('__isInitialized__', 'id', 'title', 'lft', 'rgt', 'parentId', 'level', 'children', 'comments');
     }
 }