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