Symfony 3:将字段添加到已提交的表单中

时间:2018-12-12 21:55:38

标签: forms symfony field

在查看其他主题之后,我还没有解决问题,我希望将user_id字段添加到表单中(因为用户不会选择它),但是symfony返回我“调用成员函数addEventListener( )在字符串上

这是我的代码:

SELECT Restaurants.Restaurant, Orders.Item, Orders.RequestedBy
FROM Restaurants 

LEFT JOIN (Select Restaurant, Item, RequestedBy
From [Orders] T1
Where Item=  
(Select TOP 1 Item
    from [Orders] T2
    where T1. Restaurant = T2. Restaurant
    group by Item
    order by count(Item) desc
)
group by Restaurant, Item, RequestedBy) Orders

ON  Restaurants.Restaurant = Orders.Restaurant  

1 个答案:

答案 0 :(得分:0)

确定这是个好方法吗?也许对您来说更好的方法是编写一种更新或创建$ deplacement的方法,并传递2个参数:表单中的$ data变量和user_id,如下所示:

if ($form->isSubmitted() && $form->isValid()) {

    $em = $this->getDoctrine()->getManager();
    $userId = 2; 
    $formData->userId = $userId;
    $deplacement->update($formData);
    $em->persist($deplacement);
    $em->flush($deplacement);

    return $this->redirectToRoute('deplacement_show', array('id' => $deplacement->getId()));
}

我还认为您不需要事件监听器。为什么要使用它?