【问题标题】:Symfony2 choice field not workingSymfony2 选择字段不起作用
【发布时间】:2013-05-04 01:20:30
【问题描述】:

我在这里问了一个问题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));

【问题讨论】:

    标签: symfony symfony-forms


    【解决方案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

    【讨论】:

    • 不要忘记查询生成器函数定义中的use ($catID)。
    • 仍然没有工作,我不得不将函数(EntityRepository $er)更改为函数(\Doctrine\ORM\EntityRepository $er)并且我有以下错误“预期参数类型为”Doctrine\ORM\ QueryBuilder”,“数组”给定”。这就是我避免使用实体字段的原因,因为它需要一个查询构建器实例。
    • 我知道您找到了解决方案,但对于您的用例,您应该使用实体类型字段。 findByParenting($catID) 返回什么?你能给我们看看代码吗?
    【解决方案2】:

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

    $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 指出实体字段上的选择选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 2020-10-03
      • 2014-07-05
      • 1970-01-01
      • 2014-09-01
      相关资源
      最近更新 更多