2014-10-09 73 views
0

我想在表單中使用實體類型的選擇列表,但如果向表單中添加數據,它不起作用。它給了我一個「無法將對象轉換爲int」的錯誤。使用ChoiceList實體類型

我buildForm方法

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('department', 'entity', array(
      'label' => 'Form.department', 
      'class' => 'HotfloSystemCoreBundle:Department', 
      'choice_list' => $this->departmentChoiceList, 
      'multiple' => true, 
      'required' => false, 
      'attr' => array(
       'class' => 'selectpicker', 
       'data-actions-box' => true, 
       'data-live-search' => true, 
       'data-selected-text-format' => 'count' 
      ), 
      'horizontal' => false, 
     )); 
} 

我的選擇列表

class DepartmentChoiceList extends LazyChoiceList 
{ 
    /** 
    * @var EntityManager 
    */ 
    protected $em; 

    public function __construct($em) 
    { 
     $this->em = $em; 
    } 

    /** 
    * Loads the choice list 
    * Should be implemented by child classes. 
    * 
    * @return ChoiceListInterface The loaded choice list 
    */ 
    protected function loadChoiceList() 
    { 
     $departments = $this->getDepartments(); 
     $departmentChoices = []; 
     foreach ($departments as $department) { 
      $departmentChoices[$department->getId()] = $department; 
     } 
     // Return the choice list 
     return new SimpleChoiceList($departmentChoices); 
    } 

    /** 
    * Get the departments available in the poli appointment data 
    * 
    * @return Department[] 
    */ 
    protected function getDepartments() 
    { 
     // Get the used department out of the appointment table by using a group by SQL statement 
     /** @var $qb QueryBuilder */ 
     $qb = $this->em->getRepository('MyBundle:PoliAnalyzeAppointment') 
      ->createQueryBuilder('appointment'); 
     $qb->select('DISTINCT IDENTITY(appointment.department)'); 

     // Get the actual departments 
     /** @var $qb2 QueryBuilder */ 
     $qb2 = $this->em->getRepository('MyBundle:Department') 
      ->createQueryBuilder('department'); 
     $qb2->where($qb2->expr()->in('department.id', $qb->getDQL())); 
     $qb2->orderBy('department.name', 'ASC'); 

     return $qb2->getQuery()->getResult(); 
    } 
} 

我使用的實體類型,因爲它應該被轉換爲一個實體和背部。如果我使用選擇類型,我必須自己做(我不想)。

我該如何做到這一點?

回答

1

使用query_builder選項來過濾實體選擇列表。喜歡的東西:

'query_builder' => function(EntityRepository $er) { 
    return $er->createQueryBuilder('a') 
     ->select(array('DISTINCT IDENTITY(a.department)', 'd')) 
     ->from('MyBundle:PoliAnalyzeAppointment', 'a') 
     ->innerJoin('a.department', 'd') 
     ->groupBy('a.department') 
     ->orderBy('d.name', 'ASC'); 
} 
+0

該解決方案解決了我的問題!我沒有使用innerJoin,因爲它很慢,所以我在表單中插入了實體管理器,因此我可以做一些額外的查詢。 – 2014-10-09 08:09:54

0

在buildForm方法我會與標準選擇陣列填充:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
$builder->add('department', 'entity', array(
     'label' => 'Form.department', 
     'choice_list' => $options['departmentChoiceList'], 
     'multiple' => true, 
     'required' => false, 
     'attr' => array(
      'class' => 'selectpicker', 
      'data-actions-box' => true, 
      'data-live-search' => true, 
      'data-selected-text-format' => 'count' 
     ), 
     'horizontal' => false, 
    )); 
} 

然後在同一formType添加另一種方法:

public function setDefaultOptions(OptionsResolverInterface $resolver) { 
/* 
* Instantiate DepartmentChoiceList and 
* Implement the logic to build your Array and then set it this way 
*/ 
    $resolver->setDefaults(array(
    'departmentChoiceList' => $yourDepartamentChoiceArray, 
    'data_class' => 'HotfloSystemCoreBundle:Department' 
    )); 

} 

注意:我也在這裏聲明瞭整個表單的data_class,但是如果你願意的話,你也可以把它放在setDefaults之外。 我建議看看the Entity type,所以你不必使用Transformer將對象轉換爲int - > int爲對象來選擇字段。