4

我在这里问了一个问题How to use Repository custom functions in a FormType但没有人回答,所以我做了一些挖掘和改进,但我仍然得到这个错误:

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 

现在这就是我的 CategoryType 的样子:

<?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';
    }

}

我正在从 CategoryType 访问 CategoryRepository 函数 findByParenting($parent) 并且我从函数 getAllChildren($catID) 中获取填充了准确数据的数组,但是错误在那里,我认为 Symfony 框架需要一个实体字段选择领域,但不知道如何解决它。我还更改了控制器中的 formCreate 调用,将 $this->getDoctrine() 作为 CategoryType() 的参数:

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

2 回答 2

9

好的,我设法解决了这个难题。答案很简单,我所要做的就是改变

$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--'),
                ));

并更改 getAllChildren(..) 函数,使其返回对象

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

我将其更改为:

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

    return $children;
}

非常感谢用户redbirdo指出实体字段上的选择选项。

于 2013-05-10T08:33:33.633 回答
1

好像你做的事情太复杂了。
当你写作时,你是在正确的道路上Symfony framework is expecting an entity field instead of choice field

为此,请替换:

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

经过:

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

getAllChildren($catID)(除非在其他地方使用,否则您不再需要)

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

于 2013-05-09T19:21:14.473 回答