Symfony2 select box not working

I asked a question here How to use custom Repository functions in FormType , but no one tried it, so I did a bit of work and a bit of progress, but I still get this error:

Notice: Object of class Proxies\__CG__\Kpr\CentarZdravljaBundle\Entity\Category could not be converted to int in /home/kprhr/public_html/CZ_Symfony/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php line 457 

This is what my Category type 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 as $child) { $choices[$child->getId()] = $child->getName(); } return $choices; } public function getName() { return 'category'; } } 

I call the CategoryRepository function findByParenting ($ parent) from the CategoryType type, and I get the array filled with exact data from the getAllChildren ($ catID) function, but the error is there, I think the Symfony framework expects the subject field instead of the select field, but not know how to fix it. I also modify the call to formCreate in the controller, giving $ this-> getDoctrine () as an argument for CategoryType ():

 $form = $this->createForm(new CategoryType($this->getDoctrine()), $cat, array('catID' => $id)); 
+4
source share
2 answers

Well, I managed to solve this dilemma. The answer was easy, all I had to do was change

 $builder->add('parent', 'choice', array( 'choices' => $this->getAllChildren($catID), 'required' => false, 'attr' => array('data-placeholder' => '--Izaberite Opciju--'), )); 

:

  $builder->add('parent', 'entity', array( 'class' => 'KprCentarZdravljaBundle:Category', 'choices' => $this->getAllChildren($catID), 'property' => 'name', 'required' => false, 'attr' => array('data-placeholder' => '--Izaberite Opciju--'), )); 

And change the getAllChildren (..) function so that it returns objects

 private function getAllChildren($catID) { $choices = array(); $children = $this->doctrine->getRepository('KprCentarZdravljaBundle:Category')->findByParenting($catID); foreach ($children as $child) { $choices[$child->getId()] = $child->getName(); } return $choices; } 

I changed it to:

 private function getAllChildren($catID) { $children = $this->doctrine->getRepository('KprCentarZdravljaBundle:Category')->findByParenting($catID) return $children; } 

Many thanks to redbirdo for pointing out the selection option in the entity field.

+9
source

It seems that you are doing something too complicated.
You are on the right track when writing the Symfony framework is expecting an entity field instead of choice field .

To do this, replace:

 $builder->add('parent', 'choice', array( 'choices' => $this->getAllChildren($catID), 'required' => false, 'attr' => array('data-placeholder' => '--Izaberite Opciju--'), )); 

by:

 $builder->add('users', 'entity', array( 'class' => 'KprCentarZdravljaBundle:Category', 'property' => 'name', 'query_builder' => function(EntityRepository $er) use($catID) { return $er->findByParenting($catID); }, 'required' => false, 'empty_value' => '--Izaberite Opciju--' )); 

(and you no longer need to getAllChildren($catID) if it is not used elsewhere)

http://symfony.com/doc/current/reference/forms/types/entity.html

+1
source

Source: https://habr.com/ru/post/1479929/


All Articles