Symfony2 选择字段不起作用

2024-03-18

我在这里问了一个问题如何在 FormType 中使用 Repository 自定义函数 https://stackoverflow.com/questions/16441771/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));

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

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

并更改 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 https://stackoverflow.com/users/302002/redbirdo用于指出实体字段上的选择选项。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Symfony2 选择字段不起作用 的相关文章

随机推荐