php – Doctrine Extensions更改位置超过1时,可排序无法正常工作

前端之家收集整理的这篇文章主要介绍了php – Doctrine Extensions更改位置超过1时,可排序无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Symfony 3.1 Doctrine GEDMO扩展(通过StofDoctrineExtensionsBundle).我已将我的实体设置为具有可排序行为:
  1. <?PHP
  2.  
  3. namespace AppBundle\Entity\Manual;
  4.  
  5. use AppBundle\Entity\Identifier;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9.  
  10. /**
  11. * @ORM\Table(name="manual_pages")
  12. * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
  13. */
  14. class Manual
  15. {
  16. use Identifier;
  17.  
  18. /**
  19. * @ORM\Column(type="string")
  20. * @Assert\NotBlank(message="Toto pole musí být vyplněno")
  21. */
  22. private $title;
  23.  
  24. /**
  25. * @ORM\Column(type="text")
  26. * @Assert\NotBlank(message="Toto pole musí být vyplněno")
  27. */
  28. private $content;
  29.  
  30. /**
  31. * @ORM\OneToMany(targetEntity="AppBundle\Entity\Manual\ManualImage",mappedBy="manual")
  32. * @ORM\OrderBy({"position"="ASC"})
  33. */
  34. private $images;
  35.  
  36. /**
  37. * @Gedmo\SortablePosition
  38. * @ORM\Column(type="integer",nullable=false)
  39. */
  40. private $position;
  41.  
  42. /**
  43. * @return mixed
  44. */
  45. public function getPosition()
  46. {
  47. return $this->position;
  48. }
  49.  
  50. /**
  51. * @param mixed $position
  52. */
  53. public function setPosition($position)
  54. {
  55. $this->position = $position;
  56. }
  57.  
  58.  
  59. /**
  60. * @return mixed
  61. */
  62. public function getTitle()
  63. {
  64. return $this->title;
  65. }
  66.  
  67. /**
  68. * @param mixed $title
  69. */
  70. public function setTitle($title)
  71. {
  72. $this->title = $title;
  73. }
  74.  
  75. /**
  76. * @return ManualImage[]
  77. */
  78. public function getImages()
  79. {
  80. return $this->images;
  81. }
  82.  
  83. /**
  84. * @param ManualImage[] $images
  85. */
  86. public function setImages($images)
  87. {
  88. $this->images = $images;
  89. }
  90.  
  91. /**
  92. * @return mixed
  93. */
  94. public function getContent()
  95. {
  96. return $this->content;
  97. }
  98.  
  99. /**
  100. * @param mixed $content
  101. */
  102. public function setContent($content)
  103. {
  104. $this->content = $content;
  105. }
  106.  
  107.  
  108. }

当我继续改变位置时,排序行为表现良好:

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

但是当我实现JS拖放以改变位置时,整个事情变得奇怪.例如,有这个表:

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

当我为id为6的行做时:

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

表格改为:

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

位置1没有任何东西,但位置5被占用两次.有任何想法吗?

我们很久以前就发现了这个bug.在我们的情况下,当您同时设置多个位置/刷新时出现问题.我们最终使用了没有gedmo扩展名的javascript的完整排序顺序,因为单次刷新太贵了.

另请查看以下可能相关的错误问题:

> https://github.com/Atlantic18/DoctrineExtensions/issues/1642(“更新问题”)
> https://github.com/Atlantic18/DoctrineExtensions/issues/1618
> https://github.com/Atlantic18/DoctrineExtensions/issues/1130

猜你在找的PHP相关文章