获取当前的user_id并存储它

时间:2016-02-02 10:08:27

标签: php mysql symfony controller

我想将User_id存储在我的表commande中,但它与我所做的不一致。

这是我的页面validation.html.twig的var转储,你可以看到user:null

Commandes {#745 ▼
  -id: 29
  -user: null
  -confirm: 0
  -date: DateTime {#742 ▶}
  -reference: 0
  -commande: array:6 [▶]

...
}
...
public function prepareCommandeAction()
    {
        $session = $this->getRequest()->getSession();
        $em = $this->getDoctrine()->getManager();

        if (!$session->has('commande'))
            $commande = new Commandes();
        else
            $commande = $em->getRepository('FLYBookingsBundle:Commandes')->find($session->get('commande'));

        $commande->setDate(new \DateTime());
        $commande->setUser($this->container->get('security.context')->getToken()->getUser());
        $commande->setConfirm(0);
        $commande->setReference(0);
        $commande->setCommande($this->bill());

        if (!$session->has('commande')) {
            $em->persist($commande);
            $session->set('commande',$commande);
        }

        $em->flush();

        return new Response($commande->getId());
    }

enter image description here

这是映射:

commandes.php

    /**
     * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="commandes")
     * @ORM\JoinColumn(nullable=true)
     */
    private $user;

    /**
 * Set user
 *
 * @param \Application\Sonata\UserBundle\Entity\User  $user
 * @return Commandes
 */
public function setUser(\Application\Sonata\UserBundle\Entity\User $User = null)
{
    $this->User = $User;
    return $this;

}

/**
 * Get user
 *
 * @return \Application\Sonata\UserBundle\Entity\User
 */
public function getUser()
{
    return $this->user;
}

user.php的

    public function __construct()
    {
        parent::__construct();
        $this->commandes = new \Doctrine\Common\Collections\ArrayCollection();
        $this->address = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * @ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\Commandes", mappedBy="user", cascade={"remove"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $commandes;

    /**
     * Add commandes
     *
     * @param \FLY\BookingsBundle\Entity\Commandes $commandes
     */
    public function addCommandes(\FLY\BookingsBundle\Entity\Commandes $commandes)
    {
        $this->commandes[] = $commandes;
    }

    /**
     * Remove commandes
     *
     * @param \FLY\BookingsBundle\Entity\Commandes $commandes
     */
    public function removeCommande(\FLY\BookingsBundle\Entity\Commandes $commandes)
    {
        $this->commandes->removeElement($commandes);
    }



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

0 个答案:

没有答案