使用doctrine更新列

时间:2015-04-17 20:00:27

标签: symfony doctrine-orm doctrine

我想简单更新数据库中状态的“标志”,当我点击按钮“valid”与doctrine,但值没有改变。 只需将字段状态值更新为20。 我的数据库:

enter image description here

实体需求:

/**
     * @var \Status
     *
     * @ORM\ManyToOne(targetEntity="Status")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="status_idstatus", referencedColumnName="id_status")
     * })
     */
    private $statusstatus;


  /**
 * Set statusstatus
 *
 * @param \OrderIT\Bundle\OrderBundle\Entity\Status $statusstatus
 * @return Demand
 */
public function setStatusstatus(\OrderIT\Bundle\OrderBundle\Entity\Status $statusstatus = null)
{
    $this->statusstatus = $statusstatus;

    return $this;
}

/**
 * Get statusstatus
 *
 * @return \OrderIT\Bundle\OrderBundle\Entity\Status 
 */
public function getStatusstatus()
{
    return $this->statusstatus;
}

控制器需求

    /**
 * Valid a Demand entity.
 *
 * @Route("/{idDemand}/valid", name="demand_valid")
 * @Method("GET")
 */
public function validAction($idDemand)
{
    $em = $this->getDoctrine()->getManager();
    $demand = $em->getRepository('OrderBundle:Demand')->find($idDemand);

    if (!$demand) {
        throw $this->createNotFoundException('Unable to find Demand entity.');
    }

    $demand->setStatusstatus = ('20');
    $em->persist($demand);
    $em->flush();
    return $this->redirect($this->generateUrl('listing'));
}

1 个答案:

答案 0 :(得分:3)

你这样做的方式是错误的......你必须在setter方法中插入一个状态实体:

public function validAction($idDemand)
{
    $em = $this->getDoctrine()->getManager();
    $demand = $em->getRepository('OrderBundle:Demand')->find($idDemand);
    if (!$demand) {
        throw $this->createNotFoundException('Unable to find Demand entity.');
    }

    $status= $em->getRepository('OrderBundle:Status')->find(20);
    if (!$status) {
        $status = new Status();
        $status->setId(20);
        /** or new Status(20) depends on your implementation */
    }

    $demand->setStatusstatus($status);
    $em->persist($demand);
    $em->flush();
    return $this->redirect($this->generateUrl('listing'));
}