2014-10-09 1 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, 
     )); 
} 

내 ChoiceList

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 옵션을 사용하면 엔터티 선택 목록을 필터링 할 수 있습니다. 뭔가 같은 다음 buildForm 방법에

'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을 사용하지 않았으므로 엔티티 관리자를 내 양식에 삽입하여 몇 가지 추가 쿼리를 수행 할 수 있습니다. –

0

내가 표준 옵션 배열을 채울 것입니다 :

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로 변환하여 선택 필드를 객체화 할 필요가 없다.