2016-11-19 27 views
7

使用Symfony 3.1 + Doctrine GEDMO擴展(通過StofDoctrineExtensionsBundle)。我已經把我的實體具有可排序的行爲:原則擴展當改變位置超過1時,排序不能正常工作

<?php 

namespace AppBundle\Entity\Manual; 

use AppBundle\Entity\Identifier; 
use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Gedmo\Mapping\Annotation as Gedmo; 

/** 
* @ORM\Table(name="manual_pages") 
* @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository") 
*/ 
class Manual 
{ 
    use Identifier; 

    /** 
    * @ORM\Column(type="string") 
    * @Assert\NotBlank(message="Toto pole musí být vyplněno") 
    */ 
    private $title; 

    /** 
    * @ORM\Column(type="text") 
    * @Assert\NotBlank(message="Toto pole musí být vyplněno") 
    */ 
    private $content; 

    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Manual\ManualImage", mappedBy="manual") 
    * @ORM\OrderBy({"position"="ASC"}) 
    */ 
    private $images; 

    /** 
    * @Gedmo\SortablePosition 
    * @ORM\Column(type="integer", nullable=false) 
    */ 
    private $position; 

    /** 
    * @return mixed 
    */ 
    public function getPosition() 
    { 
     return $this->position; 
    } 

    /** 
    * @param mixed $position 
    */ 
    public function setPosition($position) 
    { 
     $this->position = $position; 
    } 


    /** 
    * @return mixed 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 

    /** 
    * @param mixed $title 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 
    } 

    /** 
    * @return ManualImage[] 
    */ 
    public function getImages() 
    { 
     return $this->images; 
    } 

    /** 
    * @param ManualImage[] $images 
    */ 
    public function setImages($images) 
    { 
     $this->images = $images; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getContent() 
    { 
     return $this->content; 
    } 

    /** 
    * @param mixed $content 
    */ 
    public function setContent($content) 
    { 
     $this->content = $content; 
    } 


} 

當我進行一個改變位置的分選行爲演技OK:

$entity->setPosition($entity->getPosition() + 1); 
// or 
$entity->setPosition($entity->getPosition() - 1); 

但是,當我實現JS拖動&下降改變立場整個事情變得怪異。例如,有這個表:

id | position 
1  | 0 
2  | 1 
3  | 2 
4  | 3 
5  | 4 
6  | 5 

當我爲行做ID 6本:

$newPosition = $entity->getPosition() - 5; // = 0 
$entity->setPosition($newPosition); 

表更改爲:

id | position 
1  | 2 
2  | 3 
3  | 4 
4  | 5 
5  | 5 
6  | 0 

沒有什麼卡位1但是5號位被佔用了兩次。有任何想法嗎?

+1

似乎是相同的錯誤(或pecularity),這裏描述:http://stackoverflow.com/questions/19889652/doctrine2-sortable-update-position – SergeyLebedev

回答

4

很久以前我們也發現了這個bug。在我們的情況下,當您同時設置多個職位/沖洗時出現問題。我們最終使用了JavaScript的完整排序順序而沒有gedmo擴展名,因爲單次刷新過於昂貴。

也有看看下面的錯誤問題,這可能是相關的: