Doctrine Extensions Sortable not working correctly when changing position by more than 1
Asked Answered
T

3

12

Im using Symfony 3.1 + Doctrine GEDMO extensions (via StofDoctrineExtensionsBundle). I've set my entity to have Sortable behavior:

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


}

When i proceed to change position by one the sorting behavior is acting OK:

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

But when I've implemented JS drag&drop to change positions the whole thing gets weird. For example, having this table:

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

when I do for row with id 6 this:

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

the table changes to this:

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

There is nothing for position 1 but position 5 is occupied twice. Any ideas?

Towline answered 19/11, 2016 at 10:51 Comment(1)
Seems to be the same bug (or pecularity) that is described here: #19890152Diseuse
I
9

We also discovered this bug a long time ago. In our case there was a problem when you set multiple positions at the same time / flush. We ended up using the complete sort order of javascript without the gedmo extension because single flushes were too expensive.

Also have a look at the following bug issues which could be relevant:

Illomened answered 24/11, 2016 at 14:33 Comment(0)
A
1

Right now (october 2019) everything works perfectly as expected, I think this "bug" does not exist anymore.

Author answered 22/10, 2019 at 15:55 Comment(0)
C
0

I had similar problem and I have to add @Gedmo\SortableGroup to my ManyToOne column.

Cioffred answered 29/9, 2020 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.