Doctrine2可排序更新位置 [英] Doctrine2 Sortable Update Position

查看:150
本文介绍了Doctrine2可排序更新位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



除了数据持久性之外,所有内容似乎都能正常工作...元素和数据库中与其相关的其他元素的位置是错误的。



示例:

  ID | POSITION | TITLE 

| 1 | 0 |元素1

| 2 | 1 |元素2

| 3 | 2 |元素3

| 4 | 3 |元素4

如果我将ID 3(位置3)移动到位置0,我得到这个结果数据库:

  | ID | POSITION | TITLE 

| 1 | 2 |元素1

| 2 | 2 |元素2

| 3 | 0 |元素3

| 4 | 4 |元素4

我检查并插入的值是正确的(0)。



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

  $ pyramid = $ this-> getDoctrine() - > getRepository( 'MarquisWebsiteBundle:金字塔') - >找到($ ID); 
$ pyramid-> setPosition($ position);
$ em-> persist($ pyramid);
$ em-> flush();

如果我将元素1从位置0移动到位置1,这是很好的。



我没有在此表上使用任何 SortableGroup



编辑:



我正在使用 StofDoctrineExtensionsBundle 与Gedmo DoctrineExtension 这里是配置:

  doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping:true
映射:
gedmo_sortable:
类型:注释
前缀:Gedmo\Sortable\Entity
dir: %kernel.root_dir%/ .. / vendor / gedmo / doctrine-extensions / lib / Gedmo / Sortable / Entity
别名:GedmoSortable
is_bundle:false
gedmo_translatable:
键入:注释
前缀:Gedmo\Translatable\Entity
dir:%kernel.root_dir%/ .. / vendor / gedmo / doctrine-extensions / lib / Gedmo / Translatable / Entity
别名:GedmoTranslatable
is_bundle:false

stof_doctrine_extensions:
default_locale:en_GB
translation_fallback:true
orm:
默认值:
可排序:true
可翻译:true

实体(Pyramid.orm.yml):

  Acme\DemoBundle\Entity\Pyramid:
类型:实体
表:金字塔
字段:
id:
id:true
类型:integer
unsigned:false
可空:false
生成器:
策略:IDENTITY
位置:
类型: integer
unsigned:false
可空:false
gedmo:
- sortablePosition
标题:
类型:string
长度:255
固定:false
可空:false

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

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

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

/ **
* @var string
*
* @ ORM\Column(name =title,type =string,length = 255 ,nullable = false)
* /
private $ title;


解决方案

我想你需要包含 SortablePosition 注释:

  / ** 
* @ Gedmo\SortablePosition
* @ ORM\Column(name =position,type =integer,nullable = false)
* /
private $ 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;

解决方案

I think you need to include the SortablePosition annotation:

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

这篇关于Doctrine2可排序更新位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆