title: Doctrine2 Sortable Update Position
body: <p>I am using Doctrine 2 with Symfony 2.3 and jQuery UI sortable.</p>
  
  <p>I have a list of elements sortable with jQuery and the position is saved in the database through an Ajax request.</p>
  
  <p>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. </p>
  
  <p>Example :</p>
  
  <pre><code>| ID         | POSITION   | TITLE
  
  | 1          | 0          | Element 1
  
  | 2          | 1          | Element 2
  
  | 3          | 2          | Element 3
  
  | 4          | 3          | Element 4
  </code></pre>
  
  <p>If I am moving ID 3 (position 3) to position 0, I get this result in the database:</p>
  
  <pre><code>| ID         | POSITION   | TITLE
  
  | 1          | 2          | Element 1
  
  | 2          | 2          | Element 2
  
  | 3          | 0          | Element 3
  
  | 4          | 4          | Element 4
  </code></pre>
  
  <p>I checked and the value inserted is right (0).</p>
  
  <p>I am using this code to update the position:</p>
  
  <pre><code>$pyramid = $this-&gt;getDoctrine()-&gt;getRepository('MarquisWebsiteBundle:Pyramid')-&gt;find($id);
  $pyramid-&gt;setPosition($position);
  $em-&gt;persist($pyramid);
  $em-&gt;flush();
  </code></pre>
  
  <p>It's working well if i am moving Element 1 from position 0 to position 1.</p>
  
  <p>I am not using any <code>SortableGroup</code> on this table.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I am using <code>StofDoctrineExtensionsBundle</code> with the Gedmo <code>DoctrineExtension</code> and here is the configuration:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And the entity (Pyramid.orm.yml):</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I don't think I need to change anything in the Pyramid.php class which looks like this:</p>
  
  <pre><code>/**
   * 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;
  </code></pre>