我有一个比萨饼订购的应用程序。我有实体订单。当我提交订单时,我有问题,因为我有成分的复选框,我将其保存为数组。
实体/订单
/**
* @Assert\NotBlank(
* message="please select!")
* @Assert\NotNull(
* message="please select!")
* @ORM\Column(name="ingredients", type="array")
*
*/
protected $ingredients;
public static function getIngredientsOptions(){
return array('cheese','tomatoes','salami','onions','mushroom', 'bacon','ham','vegetables','peppers','olives');
}
DefaultController
private function buildForm($order) {
return $this->createFormBuilder($order)
->add('name','text',array('required'=>false))
->add('address','textarea',array('required'=>false))
->add('phone','number',array('required'=>false))
->add('email','text',array('required'=>false))
->add('box','choice',array(
'choices' => Orders::getBoxOptions()))
->add('ingredients','choice',array(
'choices' => Orders::getIngredientsOptions(),
'expanded' => true,
'multiple' => true))
->add('delivery','choice',array(
'choices' => Orders::getDeliveryOptions(),
'expanded' => true))
->add('save','submit')
->getForm();
}
/**
* @Route("/new", name="new_order")
* @Template()
*/
public function orderAction(Request $request)
{
$order = new Orders();
$form = $this->buildForm($order);
$form->handleRequest($request);
if ($form->isValid()) {
$order = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->forward('S1500238090PizzaBundle:Default:index');
}
return array('form' => $form->createView());
}
index.html.twig
<h3>Orders</h3>
<table border>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
<th>E-Mail</th>
<th>Box</th>
<th>Ingredients</th>
<th>Delivery</th>
</tr>
{% for order in orders %}
<tr>
<td>{{order.id}}</td>
<td>{{order.name}}</td>
<td>{{order.address}}</td>
<td>{{order.phone}}</td>
<td>{{order.email}}</td>
<td>{{boxOptions[order.box]}}</td>
<td>{{ingredientsOptions[order.ingredients]}}</td>
<td>{{deliveryOptions[order.delivery]}}</td>
<td>
<a href="{{path('edit_order',{'id':order.id})}}">Edit</a>
<a href="{{path('delete_order',{'id':order.id})}}">Delete</a>
</td>
</tr>
{% endfor %}
</table>
<a href="{{path('new_order')}}">New order</a>
我不知道如何正确保存配料阵列,然后再渲染它。
答案 0 :(得分:0)
我发现,我必须使用以下内容更改index.html.twig成分标记:
<td>{% for ingredient in order.ingredients %}
{{ingredient}}
{% endfor %}
</td>