可排序行为(Doctrine扩展名):未在数据库中保存的位置

时间:2014-09-29 14:10:36

标签: symfony

我在控制器中有这个:

  $em = $this->getDoctrine()->getManager();
  $repository = $this->getDoctrine()->getRepository('ProjectFrontendBundle:Item');

  $item = $repository->find(3);
  $item->setPosition('10');
  var_dump($item->getPosition()); exit(); //this returns 10

  $em->persist($item);
  $em->flush();

  var_dump($item->getPosition()); exit();//this returns 5 

  return $this->render('ProjectBackendBundle:Default:prueba.html.twig');

问题:位置(10)的新值未保存在数据库(MySQL)中。

我得到这个日志:

 1207 Query SELECT t0.id AS id1, t0.position AS position2, t0.image_name AS image_name3, t0.updatedAt AS updatedAt4, t0.slug AS slug5, t0.nombre AS nombre6, t0.activado AS activado7, t0.parent_id AS parent_id8 FROM Item t0 WHERE t0.id = 3
 1207 Query SELECT MAX(i0_.position) AS sclr0 FROM Item i0_
 1207 Query START TRANSACTION
 1207 Query UPDATE Item SET position = 5 WHERE id = 3
 1207 Query commit
 1207 Quit  

这是我的实体Item

<?php
namespace Project\FrontendBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * Project\FrontendBundle\Entity\Item
 *
 * @ORM\Table
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
 * @Vich\Uploadable
 */
class Item
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Item", mappedBy="parent")
     */
    private $children;

    /**
     * @ORM\ManyToOne(targetEntity="Item", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="Subitem", mappedBy="item")
     */
    private $subitems;

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

    /**
     * @Assert\File(
     *     maxSize="20M",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )
     * @Vich\UploadableField(mapping="item_image", fileNameProperty="imageName")
     *
     * @var File $image
     */
    protected $image;

    /**
     * @ORM\Column(type="string", length=255, name="image_name", nullable=true)
     *
     * @var string $imageName
     */
    protected $imageName;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     *
     * @var \DateTime $updatedAt
     */
    protected $updatedAt;

    /**
     * @Gedmo\Slug(fields={"nombre"})
     * @ORM\Column(length=128, unique=true)
     */
    private $slug;

    /**
     * @ORM\Column(type="string", length=255, name="nombre")
     */
    protected $nombre;

    /**
     * @ORM\Column(type="boolean", name="activado")
     */
    protected $activado = true;


    public function __toString()
    {
        return $this->nombre;
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->children = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set position
     *
     * @param integer $position
     * @return Item
     */
    public function setPosition($position)
    {
        $this->position = $position;

        return $this;
    }

    /**
     * Get position
     *
     * @return integer 
     */
    public function getPosition()
    {
        return $this->position;
    }

    /**
     * Set imageName
     *
     * @param string $imageName
     * @return Item
     */
    public function setImageName($imageName)
    {
        $this->imageName = $imageName;

        return $this;
    }

    /**
     * Get imageName
     *
     * @return string 
     */
    public function getImageName()
    {
        return $this->imageName;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     * @return Item
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Item
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * Get slug
     *
     * @return string 
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * Set nombre
     *
     * @param string $nombre
     * @return Item
     */
    public function setNombre($nombre)
    {
        $this->nombre = $nombre;

        return $this;
    }

    /**
     * Get nombre
     *
     * @return string 
     */
    public function getNombre()
    {
        return $this->nombre;
    }

    /**
     * Set activado
     *
     * @param boolean $activado
     * @return Item
     */
    public function setActivado($activado)
    {
        $this->activado = $activado;

        return $this;
    }

    /**
     * Get activado
     *
     * @return boolean 
     */
    public function getActivado()
    {
        return $this->activado;
    }

    /**
     * Add children
     *
     * @param \Project\FrontendBundle\Entity\Item $children
     * @return Item
     */
    public function addChild(\Project\FrontendBundle\Entity\Item $children)
    {
        $this->children[] = $children;

        return $this;
    }

    /**
     * Remove children
     *
     * @param \Project\FrontendBundle\Entity\Item $children
     */
    public function removeChild(\Project\FrontendBundle\Entity\Item $children)
    {
        $this->children->removeElement($children);
    }

    /**
     * Get children
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * Set parent
     *
     * @param \Project\FrontendBundle\Entity\Item $parent
     * @return Item
     */
    public function setParent(\Project\FrontendBundle\Entity\Item $parent = null)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     *
     * @return \Project\FrontendBundle\Entity\Item 
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Add subitems
     *
     * @param \Project\FrontendBundle\Entity\Subitem $subitems
     * @return Item
     */
    public function addSubitem(\Project\FrontendBundle\Entity\Subitem $subitems)
    {
        $this->subitems[] = $subitems;

        return $this;
    }

    /**
     * Remove subitems
     *
     * @param \Project\FrontendBundle\Entity\Subitem $subitems
     */
    public function removeSubitem(\Project\FrontendBundle\Entity\Subitem $subitems)
    {
        $this->subitems->removeElement($subitems);
    }

    /**
     * Get subitems
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSubitems()
    {
        return $this->subitems;
    }
}

我没有在apache或symfony日志中收到错误。

1 个答案:

答案 0 :(得分:0)

问题是因为Doctrine Sortable行为。我有4个元素,当我尝试给出一个高于5(6,7,8,9等)的位置时,它将它转换为5,因为我的上一个位置是4。