엔티티 유형이있는 양식에서 선택 목록을 사용하려고하지만 양식에 데이터를 추가해도 작동하지 않습니다. 그것은 나에게 "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();
}
}
가 다시 엔티티로 변환해야하기 때문에이 개체 유형을 사용하고 있습니다. 선택 유형을 사용하는 경우이 작업을 직접 수행해야합니다 (원하지 않는 작업).
어떻게하면됩니까?
이 솔루션은 내 문제를 해결했습니다. 천천히 움직이기 때문에 innerJoin을 사용하지 않았으므로 엔티티 관리자를 내 양식에 삽입하여 몇 가지 추가 쿼리를 수행 할 수 있습니다. –