Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 Sortable Update Position

I am using Doctrine 2 with Symfony 2.3 and jQuery UI sortable.

I have a list of elements sortable with jQuery and the position is saved in the database through an Ajax request.

All seem to work perfectly except the data persistence... The position of the element and the other elements related to it in the database is wrong.

Example :

| ID         | POSITION   | TITLE

| 1          | 0          | Element 1

| 2          | 1          | Element 2

| 3          | 2          | Element 3

| 4          | 3          | Element 4

If I am moving ID 3 (position 3) to position 0, I get this result in the database:

| ID         | POSITION   | TITLE

| 1          | 2          | Element 1

| 2          | 2          | Element 2

| 3          | 0          | Element 3

| 4          | 4          | Element 4

I checked and the value inserted is right (0).

I am using this code to update the position:

$pyramid = $this->getDoctrine()->getRepository('MarquisWebsiteBundle:Pyramid')->find($id);
$pyramid->setPosition($position);
$em->persist($pyramid);
$em->flush();

It's working well if i am moving Element 1 from position 0 to position 1.

I am not using any SortableGroup on this table.

EDIT:

I am using StofDoctrineExtensionsBundle with the Gedmo DoctrineExtension and here is the configuration:

doctrine:
    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true
        mappings:
            gedmo_sortable:
                type: annotation
                prefix: Gedmo\Sortable\Entity
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Sortable/Entity"
                alias: GedmoSortable
                is_bundle: false
            gedmo_translatable:
                type: annotation
                prefix: Gedmo\Translatable\Entity
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
                alias: GedmoTranslatable
                is_bundle: false

stof_doctrine_extensions:
    default_locale: en_GB
    translation_fallback: true
    orm:
        default:
            sortable: true
            translatable: true

And the entity (Pyramid.orm.yml):

Acme\DemoBundle\Entity\Pyramid:
    type: entity
    table: pyramid
    fields:
        id:
            id: true
            type: integer
            unsigned: false
            nullable: false
            generator:
                strategy: IDENTITY
        position:
            type: integer
            unsigned: false
            nullable: false
            gedmo:
              - sortablePosition
        title:
            type: string
            length: 255
            fixed: false
            nullable: false

I don't think I need to change anything in the Pyramid.php class which looks like this:

/**
 * Pyramid
 *
 * @ORM\Table(name="pyramid")
 * @ORM\Entity
 */
class Pyramid
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
     */
    private $title;
like image 914
pjehan Avatar asked Nov 10 '13 12:11

pjehan


1 Answers

I think you need to include the SortablePosition annotation:

/**
 * @Gedmo\SortablePosition
 * @ORM\Column(name="position", type="integer", nullable=false)
 */
private $position;
like image 158
Vova Solop Avatar answered Oct 18 '22 05:10

Vova Solop