消除从数据库生成的重复值到组合框中

时间:2017-06-30 10:27:11

标签: symfony combobox doctrine twig

我想从comobox中删除重复的产品名称,该值是从数据库(Product entity)生成的

buildform中是否有配置可以消除重复值?或者有一些代码可以编写?

更新

我想消除用户的重复值,令人困惑的是有两个相同的值,我的逻辑是正确的(在我的情况下)我可以有两个同名的产品。我必须做的事情:用户选择产品的名称,并根据它更新它的颜色

我的控制器:

public function addPoductAction(Request $request) { 
$form = $this->createForm('AppBundle\Form\ProductType');
$form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $productcombo=$form[name]->getData();
        $productcomboName = $ productcombo ->getName();
        $color=$form[‘color’]->getData();

$em = $this->getDoctrine()->getManager();
 $repository = $this
     ->getDoctrine()
     ->getManager()
     ->getRepository('AppBundle:Product');
 $products = $repository->findBy(
     array('name' => $productcomboName) 
 );
foreach ($products as $product) {
    $product ->setColor($color);
    $em->flush();
}
return $this->redirectToRoute(‘Product_page’);
}
return $this->render('AppBundle:Product:updateProducts.html.twig', array(
    'form' => $form->createView(),
));
}

产品实体:

class Product
{
    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255,nullable=true)
     */
    private $name;
}

产品类型:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name',EntityType::class,array(
        'class' => 'AppBundle:Product', 'choice_label'=>'name'     ))

}

Twig:

<label Product name < </label>
<div>
    {{ form_widget(form.name)}}
</div>

enter image description here

0 个答案:

没有答案
相关问题