Php 获取并存储当前用户id
我想在我的表命令中存储用户_id,但它与我所做的不兼容 这是mypage validation.html.twig的var转储,您可以看到user:nullPhp 获取并存储当前用户id,php,mysql,symfony,controller,Php,Mysql,Symfony,Controller,我想在我的表命令中存储用户_id,但它与我所做的不兼容 这是mypage validation.html.twig的var转储,您可以看到user:null Commandes {#745 ▼ -id: 29 -user: null -confirm: 0 -date: DateTime {#742 ▶} -reference: 0 -commande: array:6 [▶] ... } ... public function prepareCommandeActio
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());
}
这是映射:
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;
}
您是否尝试过只使用controller方法
controller::getUser()
?你有实体上的关系设置吗?我们能看到映射代码吗?现在我将把映射放在我的帖子的顶部。但是当我进行模式验证时,映射是good@DevDonkey你看到映射代码了吗?你确定要多个用户使用一条命令吗?(对不起,我不知道命令是什么)。是的,我想要很多用户,因为命令就是命令。当用户添加产品时,他可以验证的是order->[commande],并且每个订单都必须有一个用户ID。您是否尝试过仅使用控制器方法controller::getUser()
?你有实体上的关系设置吗?我们能看到映射代码吗?现在我将把映射放在我的帖子的顶部。但是当我进行模式验证时,映射是good@DevDonkey你看到映射代码了吗?你确定要多个用户使用一条命令吗?(对不起,我不知道命令是什么)。是的,我想要很多用户,因为命令就是命令。当用户添加产品时,他可以验证的是订单->[commande],并且每个订单都必须有一个用户id