symfony - Symfony2 choice field not working -
i asked question here how use repository custom functions in formtype nobody anwsered, did little digging , advanced little still error:
notice: object of class proxies\__cg__\kpr\centarzdravljabundle\entity\category not converted int in /home/kprhr/public_html/cz_symfony/vendor/symfony/symfony/src/symfony/component/form/extension/core/choicelist/choicelist.php line 457
now how categorytype looks like:
<?php namespace kpr\centarzdravljabundle\form\type; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface; use symfony\bridge\doctrine\registryinterface; class categorytype extends abstracttype { private $doctrine; public function __construct(registryinterface $doctrine) { $this->doctrine = $doctrine; } public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'kpr\centarzdravljabundle\entity\category', 'catid' => null, )); } public function buildform(formbuilderinterface $builder, array $options) { $someid = $builder->getdata()->getid(); $param = ($someid) ? $someid : 0; $catid = $options['catid']; $builder->add('name', 'text', array('attr' => array('class' => 'span6'))); $builder->add('file', 'file', array('image_path' => 'webpath', 'required' => false)); $builder->add('parent', 'choice', array( 'choices' => $this->getallchildren($catid), 'required' => false, 'attr' => array('data-placeholder' => '--izaberite opciju--'), )); $builder->add('tags', 'tag_selector', array( 'required' => false, )); $builder->add('status', 'choice', array( 'choices' => array('1' => 'aktivna', '0' => 'neaktivna'), 'required' => true, )); $builder->add('queue', 'text', array('attr' => array('class' => 'span3'))); } private function getallchildren($catid) { $choices = array(); $children = $this->doctrine->getrepository('kprcentarzdravljabundle:category')->findbyparenting($catid); foreach ($children $child) { $choices[$child->getid()] = $child->getname(); } return $choices; } public function getname() { return 'category'; } }
i accessing categoryrepository function findbyparenting($parent) categorytype , getting array populated accurate data function getallchildren($catid) error there, think symfony framework expecting entity field instead of choice field, dont know how fix it. changet formcreate call in controller giving $this->getdoctrine() argument categorytype():
$form = $this->createform(new categorytype($this->getdoctrine()), $cat, array('catid' => $id));
ok managed resolve dilemma. answer easy had change
$builder->add('parent', 'choice', array( 'choices' => $this->getallchildren($catid), 'required' => false, 'attr' => array('data-placeholder' => '--izaberite opciju--'), ));
to this:
$builder->add('parent', 'entity', array( 'class' => 'kprcentarzdravljabundle:category', 'choices' => $this->getallchildren($catid), 'property' => 'name', 'required' => false, 'attr' => array('data-placeholder' => '--izaberite opciju--'), ));
and change getallchildren(..) function returns objects
private function getallchildren($catid) { $choices = array(); $children = $this->doctrine->getrepository('kprcentarzdravljabundle:category')->findbyparenting($catid); foreach ($children $child) { $choices[$child->getid()] = $child->getname(); } return $choices; }
i changed to:
private function getallchildren($catid) { $children = $this->doctrine->getrepository('kprcentarzdravljabundle:category')->findbyparenting($catid) return $children; }
lots of user redbirdo pointing out choices option on entity field.
Comments
Post a Comment