Преглед на файлове

[Sortable] null reference when not using SortableGroup (fix #219)

Lukas Botsch преди 13 години
родител
ревизия
8f4bc1033b
променени са 3 файла, в които са добавени 98 реда и са изтрити 12 реда
  1. 18 11
      lib/Gedmo/Sortable/SortableListener.php
  2. 55 0
      tests/Gedmo/Sortable/Fixture/SimpleListItem.php
  3. 25 1
      tests/Gedmo/Sortable/SortableTest.php

+ 18 - 11
lib/Gedmo/Sortable/SortableListener.php

@@ -104,8 +104,10 @@ class SortableListener extends MappedEventSubscriber
 
         // Get groups
         $groups = array();
-        foreach ($config['groups'] as $group) {
-            $groups[$group] = $meta->getReflectionProperty($group)->getValue($object);
+        if (isset($config['groups'])) {
+            foreach ($config['groups'] as $group) {
+                $groups[$group] = $meta->getReflectionProperty($group)->getValue($object);
+            }
         }
         // Get hash
         $hash = $this->getHash($meta, $groups, $object, $config);
@@ -167,11 +169,13 @@ class SortableListener extends MappedEventSubscriber
 
         // Get groups
         $groups = array();
-        foreach ($config['groups'] as $group) {
-            $changed = $changed ||
-                (array_key_exists($group, $changeSet)
-                    && $changeSet[$group][0] != $changeSet[$group][1]);
-            $groups[$group] = $meta->getReflectionProperty($group)->getValue($object);
+        if (isset($config['groups'])) {
+            foreach ($config['groups'] as $group) {
+                $changed = $changed ||
+                    (array_key_exists($group, $changeSet)
+                        && $changeSet[$group][0] != $changeSet[$group][1]);
+                $groups[$group] = $meta->getReflectionProperty($group)->getValue($object);
+            }
         }
 
         if (!$changed) return;
@@ -245,8 +249,10 @@ class SortableListener extends MappedEventSubscriber
 
         // Get groups
         $groups = array();
-        foreach ($config['groups'] as $group) {
-            $groups[$group] = $meta->getReflectionProperty($group)->getValue($object);
+        if (isset($config['groups'])) {
+            foreach ($config['groups'] as $group) {
+                $groups[$group] = $meta->getReflectionProperty($group)->getValue($object);
+            }
         }
         // Get hash
         $hash = $this->getHash($meta, $groups, $object, $config);
@@ -313,10 +319,11 @@ class SortableListener extends MappedEventSubscriber
     private function getMaxPosition($em, $meta, $config, $object)
     {
         $maxPos = null;
+        $groups = isset($config["groups"]) ? $config["groups"] : array();
         $qb = $em->createQueryBuilder();
         $qb->select('MAX(n.'.$config['position'].')')
            ->from($config['useObjectClass'], 'n');
-        $qb = $this->addGroupWhere($qb, $config["groups"], $meta, $object);
+        $qb = $this->addGroupWhere($qb, $groups, $meta, $object);
         $query = $qb->getQuery();
         $query->useQueryCache(false);
         $query->useResultCache(false);
@@ -377,4 +384,4 @@ class SortableListener extends MappedEventSubscriber
     {
         return __NAMESPACE__;
     }
-}
+}

+ 55 - 0
tests/Gedmo/Sortable/Fixture/SimpleListItem.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace Sortable\Fixture;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
+ */
+class SimpleListItem
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(type="string", length=255)
+     */
+    private $name;
+
+    /**
+     * @Gedmo\SortablePosition
+     * @ORM\Column(type="integer")
+     */
+    private $position;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setName($name)
+    {
+        $this->name = $name;
+    }
+
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    public function setPosition($position)
+    {
+        $this->position = $position;
+    }
+
+    public function getPosition()
+    {
+        return $this->position;
+    }
+}

+ 25 - 1
tests/Gedmo/Sortable/SortableTest.php

@@ -7,6 +7,7 @@ use Tool\BaseTestCaseORM;
 use Sortable\Fixture\Node;
 use Sortable\Fixture\Item;
 use Sortable\Fixture\Category;
+use Sortable\Fixture\SimpleListItem;
 
 /**
  * These are tests for sluggable behavior
@@ -21,6 +22,7 @@ class SortableTest extends BaseTestCaseORM
     const NODE = 'Sortable\\Fixture\\Node';
     const ITEM = 'Sortable\\Fixture\\Item';
     const CATEGORY = 'Sortable\\Fixture\\Category';
+    const SIMPLE_LIST_ITEM = 'Sortable\\Fixture\\SimpleListItem';
     private $nodeId;
     
     protected function setUp()
@@ -255,13 +257,35 @@ class SortableTest extends BaseTestCaseORM
         $this->assertEquals("Item2_2", $items[1]->getName());
         $this->assertEquals("Category2", $items[1]->getCategory()->getName());
     }
+
+    /**
+     * Test for issue #219
+     */
+    public function test219()
+    {
+        $item1 = new SimpleListItem();
+        $item1->setName("Item 1");
+        $this->em->persist($item1);
+
+        $this->em->flush();
+        
+        $item1->setName("Update...");
+        $item1->setPosition(1);
+        $this->em->persist($item1);
+        $this->em->flush();
+        
+        $this->em->remove($item1);
+        $this->em->flush();
+        $this->em->clear();
+    }
     
     protected function getUsedEntityFixtures()
     {
         return array(
             self::NODE,
             self::ITEM,
-            self::CATEGORY
+            self::CATEGORY,
+            self::SIMPLE_LIST_ITEM,
         );
     }