Friday 15 May 2015

symfony2 - Use ChoiceList for entity type -



symfony2 - Use ChoiceList for entity type -

i'm trying utilize selection list in form entity type it's not working if add together info form. it's giving me "could not convert object int" error.

my buildform method

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, )); }

my choicelist

class departmentchoicelist extends lazychoicelist { /** * @var entitymanager */ protected $em; public function __construct($em) { $this->em = $em; } /** * loads selection list * should implemented kid classes. * * @return choicelistinterface loaded selection list */ protected function loadchoicelist() { $departments = $this->getdepartments(); $departmentchoices = []; foreach ($departments $department) { $departmentchoices[$department->getid()] = $department; } // homecoming selection list homecoming new simplechoicelist($departmentchoices); } /** * departments available in poli appointment info * * @return department[] */ protected function getdepartments() { // used section out of appointment table using grouping sql statement /** @var $qb querybuilder */ $qb = $this->em->getrepository('mybundle:polianalyzeappointment') ->createquerybuilder('appointment'); $qb->select('distinct identity(appointment.department)'); // 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'); homecoming $qb2->getquery()->getresult(); } }

i'm using entity type because should converted entity , back. if utilize selection type have myself (which don't want).

how can accomplish this?

use query_builder alternative filter entity selection list. like:

'query_builder' => function(entityrepository $er) { homecoming $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'); }

symfony2 symfony-forms

No comments:

Post a Comment