簡體   English   中英

Symfony2實體字段類型當前選擇

[英]Symfony2 Entity Field Type current choices

我正在嘗試在表單中創建實體字段類型。 這是代碼:

$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();

這是來自$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

你覺得有什么不對嗎? 因為它正在生成下拉列表但不選擇“已選擇”值。

賦給'choices'索引的對象( $typeavailable )應該與賦給'data'的SINGLE對象屬於同一個類。 目前你正在給一個拿着錯誤對象的array 為什么單個物體? 因為您的表單僅支持1個選定項( 'multiple' => false, )。

用此來解決問題:

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

這段代碼應該為您提供您想要選擇的Specs對象。

如果您的選擇查詢也返回多於1個對象,您可能想要在您的關系或查詢中重做某些內容。 如果您不想這樣做,您仍然可以使用以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM