【问题标题】:Doctrine Extensions Sortable not working correctly when changing position by more than 1当位置改变超过 1 时,Doctrine Extensions Sortable 无法正常工作
【发布时间】:2016-11-19 10:51:20
【问题描述】:

我正在使用 Symfony 3.1 + Doctrine GEDMO 扩展(通过 StofDoctrineExtensionsBundle)。我已将我的实体设置为具有可排序行为:

<?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;
    }


}

当我继续将位置更改为一个时,排序行为正常:

$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 被占用了两次。有什么想法吗?

【问题讨论】:

标签: php symfony doctrine-extensions stofdoctrineextensions


【解决方案1】:

我们很久以前也发现了这个错误。在我们的例子中,当您同时设置多个位置/刷新时会出现问题。我们最终使用了没有 gedmo 扩展的 JavaScript 的完整排序顺序,因为单次刷新太昂贵了。

还可以查看以下可能相关的错误问题:

【讨论】:

    【解决方案2】:

    现在(2019 年 10 月)一切正常,我认为这个“错误”已经不存在了。

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,我必须将 @Gedmo\SortableGroup 添加到我的 ManyToOne 列中。

      【讨论】:

        猜你喜欢
        • 2020-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-01
        • 2013-11-13
        • 2021-04-29
        相关资源
        最近更新 更多