Doctrine2可排序更新位置

pje*_*han 6 jquery-ui jquery-ui-sortable symfony doctrine-orm

我正在使用Doctrine 2和Symfony 2.3以及jQuery UI进行排序.

我有一个可以使用jQuery排序的元素列表,并且位置通过Ajax请求保存在数据库中.

除了数据持久性之外,所有似乎都工作得很好......元素的位置和数据库中与其相关的其他元素是错误的.

示例:

| ID         | POSITION   | TITLE

| 1          | 0          | Element 1

| 2          | 1          | Element 2

| 3          | 2          | Element 3

| 4          | 3          | Element 4
Run Code Online (Sandbox Code Playgroud)

如果我将ID 3(位置3)移动到位置0,我将在数据库中得到以下结果:

| ID         | POSITION   | TITLE

| 1          | 2          | Element 1

| 2          | 2          | Element 2

| 3          | 0          | Element 3

| 4          | 4          | Element 4
Run Code Online (Sandbox Code Playgroud)

我检查了,插入的值是正确的(0).

我正在使用此代码更新位置:

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

如果我将元素1从位置0移动到位置1,它运行良好.

我没有SortableGroup在这张桌子上使用任何东西.

编辑:

我正在使用StofDoctrineExtensionsBundleGedmo DoctrineExtension,这里是配置:

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
Run Code Online (Sandbox Code Playgroud)

和实体(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
Run Code Online (Sandbox Code Playgroud)

我不认为我需要更改Pyramid.php类中的任何内容,如下所示:

/**
 * 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;
Run Code Online (Sandbox Code Playgroud)

小智 2

我认为你需要包含注释SortablePosition

/**
 * @Gedmo\SortablePosition
 * @ORM\Column(name="position", type="integer", nullable=false)
 */
private $position;
Run Code Online (Sandbox Code Playgroud)