Arrays 从条令查询加载Symfony FormBuilder“选项”

Arrays 从条令查询加载Symfony FormBuilder“选项”,arrays,forms,symfony,doctrine-orm,Arrays,Forms,Symfony,Doctrine Orm,我试图用下面的代码生成一个表单元素。我尝试输入的产品数组包含我的产品实体的所有唯一值 我的产品和订单实体在原则中具有多对多关系设置: use FOS\RestBundle\Controller\FOSRestController; use AppBundle\Entity\Product; class DefaultController extends FOSRestController { /** * @Route("/newOrder", name="new_order

我试图用下面的代码生成一个表单元素。我尝试输入的产品数组包含我的产品实体的所有唯一值

我的产品和订单实体在原则中具有多对多关系设置:

use FOS\RestBundle\Controller\FOSRestController;
use AppBundle\Entity\Product;

class DefaultController extends FOSRestController
{

    /**
     * @Route("/newOrder", name="new_order")
     */
    public function newOrderAction(Request $request)
    {
        $order = new Order();
        $em = $this->getDoctrine()->getManager();

        $products = $em->getRepository('AppBundle:Product')
            ->findAllOrderedByName();

        $form = $this->CreateFormBuilder($order)
            ->add('products','choice', array(
                    'choices' => $products,
                'multiple' => true,
            ))
            ->add('Save','submit',array('label'=>'Update Order'))
            ->getForm();
    }
}
但是,我不明白为什么返回的表单会显示每个选项的product2,并生成标签,这使得下拉菜单有点笨拙

<select id="form_products" name="form[products]">
    <optgroup label="0">
        <option value="id" >Product#2</option></optgroup>
    <optgroup label="1">
        <option value="id" >Product#2</option></optgroup>
</select>

您不应使用“选择”表单字段,而应使用“实体”字段,并使用实体表单字段的“查询生成器”选项来获取按描述订购的产品。

您不应使用“选择”表单字段,而应使用“实体”字段。并使用实体表单字段的“查询生成器”选项来获取订购的产品description@Viktor77-希望我能接受这个评论作为回答,因为我正在重新启动和运行:-ThanksHi@Bendy,我将把它作为回答发布,这样你就可以接受它,它可能对更多的人在未来有用。
public function findAllOrderedByName()
{
    return $this->getEntityManager()
        ->createQuery(
            'SELECT p.description FROM AppBundle:Product p ORDER BY p.description ASC'
        )
        ->getResult();
}