Adriano Pedro
Adriano Pedro

Reputation: 437

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`:

$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.

Upvotes: 3

Views: 1075

Answers (1)

Mats Rietdijk
Mats Rietdijk

Reputation: 2576

The objects given to the 'choices' index ($typeavailable) should be of the same class as the SINGLE object given to 'data'. At the moment you are giving back an array holding the wrong object. Why a single object? Because your form supports only 1 selected item ('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.

If your select query also returns more then 1 object you might want to redo something in your relationships or query. If you don't want to do that you can still use the following:

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

Upvotes: 1

Related Questions