简体   繁体   English

Symfony2实体字段类型当前选择

[英]Symfony2 Entity Field Type current choices

I'm trying to create an entity field type in form. 我正在尝试在表单中创建实体字段类型。 Here is the code: 这是代码:

$extraSpecsRepository = $this -> getDoctrine() 
                              -> getRepository('LabsCatalogBundle:Specs'); 
$availQuery = $extraSpecsRepository->createQueryBuilder('sel')
                ->where("sel.cat = '0'")
                ->getQuery();
$available = $availQuery->getResult();


$extraSpecsRepository = $this -> getDoctrine() 
                              -> getRepository('LabsCatalogBundle:ProductExtraspecs'); 
$selQuery = $extraSpecsRepository->createQueryBuilder('sel')
                  ->join('sel.specs', 'specs')
                  ->where("specs.cat = '0' AND sel.regmatid = $id")
                  ->getQuery();
$selected = $selQuery->getResult();



$form = $this ->createFormBuilder($product)
                ->add('extraspecs', 'entity', array(
                                'class' => 'LabsCatalogBundle:Specs',
                                'choices' => $typeavailable,
                                'data' => $selected,
                                'property' => 'specid',
                                'multiple' => false,
                            ))
                ->getForm();

And this is the var_dump from both $selected and $typeavailable variables`: 这是来自$selected$typeavailable变量的var_dump

$typeavailable:    
array (size=4)
      0 => 
        array (size=4)
          'specid' => int 20
          'desc' => string 'Primary Antibodies' (length=18)
          'cat' => int 0
          'type' => int 1
      1 => 
        array (size=4)
          'specid' => int 21
          'desc' => string 'Secondary Antibodies' (length=20)
          'cat' => int 0
          'type' => int 2
      2 => 
        array (size=4)
          'specid' => int 22
          'desc' => string 'Fluorescent-Labeled Antibodies' (length=30)
          'cat' => int 0
          'type' => int 5
      3 => &
        array (size=4)
          'specid' => int 27
          'desc' => string 'Related Antibodies' (length=18)
          'cat' => int 0
          'type' => int 7

$selected:
    array (size=1)
      0 => &
        array (size=4)
          'regmatid' => int 1600
          'specid' => int 21
          'attrib' => null
          'value' => null

Do you see anything wrong? 你觉得有什么不对吗? Because it is generating the droplist but not choosing the 'selected' value. 因为它正在生成下拉列表但不选择“已选择”值。

The objects given to the 'choices' index ( $typeavailable ) should be of the same class as the SINGLE object given to 'data' . 赋给'choices'索引的对象( $typeavailable )应该与赋给'data'的SINGLE对象属于同一个类。 At the moment you are giving back an array holding the wrong object. 目前你正在给一个拿着错误对象的array Why a single object? 为什么单个物体? Because your form supports only 1 selected item ( 'multiple' => false, ). 因为您的表单仅支持1个选定项( 'multiple' => false, )。

Use this to fix the problem: 用此来解决问题:

$result = $selQuery->getSingleResult()->getSpecs();
$selected = $result[0];

This piece of code should give you the Specs object you want selected. 这段代码应该为您提供您想要选择的Specs对象。

If your select query also returns more then 1 object you might want to redo something in your relationships or query. 如果您的选择查询也返回多于1个对象,您可能想要在您的关系或查询中重做某些内容。 If you don't want to do that you can still use the following: 如果您不想这样做,您仍然可以使用以下内容:

$results = $selQuery->getResult();
$result = $results[0]->getSpecs();
$selected = $result[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM