来自数据库 SYMFONY 2 的选择表格


Choice form from database SYMFONY 2

我从symfony 2开始,我想显示一个包含数据库中数据的"选择"类型,但我有一个问题:

添加操作 :

    public function addAction()
{
    $categories = new CategoriesAnnonce();
    $form = $this->get('form.factory')->create(new AddFormType($categories),$categories);
    return $this->render('AnnoncesBundle::add.html.twig', array(
        'form' => $form->createView(),
    ));
}

添加窗体类型.php :

    <?php
namespace AnnoncesBundle'Form'Type;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
use Symfony'Component'OptionsResolver'OptionsResolverInterface;
use Symfony'Component'Validator'Constraints as Assert;
class AddFormType extends AbstractType
{
    private $cat;
    public function __construct(CategoriesAnnonce $categories)
    {
        $this->cat = $categories->getNomSousCategorie();
    }
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('titre', 'text')
            ->add('categories', 'choice', array(
            'choices' => $this->cat,
        ));
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AnnoncesBundle'Form'Model'Add',
        ));
    }
    public function getName()
    {
        return 'Add';
    }
}
?>

错误:

Catchable Fatal Error: Argument 1 passed to AnnoncesBundle'Form'Type'AddFormType::__construct() must be an instance of AnnoncesBundle'Form'Type'CategoriesAnnonce, instance of AnnoncesBundle'Entity'CategoriesAnnonce given, called in /Users/jordan/Desktop/www/Lesbonnesaffaires/src/AnnoncesBundle/Controller/DefaultController.php on line 69 and defined

它使用文件中定义的命名空间,而不是CategoriesAnnonce所属的正确命名空间。添加use语句:

use AnnoncesBundle'Entity'CategoriesAnnonce

或者将类型提示更改为 FQCN,例如:

public function __construct('AnnoncesBundle'Entity'CategoriesAnnonce, $categories) {
  //...
}

谢谢,但我现在有另一个问题:

窗体的视图数据应是类 AnnoncesBundle''Form''Model''Add 的实例,但它是类 AnnoncesBundle''Entity''CategoriesAnnonce 的实例。您可以通过将"data_class"选项设置为 null 或添加将类 AnnoncesBundle''Entity''CategoriesAnnonce 的实例转换为 AnnoncesBundle''Form''Model''Add 实例的视图转换器来避免此错误。

发生这种情况是因为您将data_class设置为 AnnoncesBundle'Form'Model'Add

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AnnoncesBundle'Form'Model'Add',
    ));
}

但是您正在尝试将其传递AnnoncesBundle'Entity'CategoriesAnnonce

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('titre', 'text')
        // this cat is an instance of AnnoncesBundle'Entity'CategoriesAnnonce
        ->add('categories', 'choice', array(
        'choices' => $this->cat,
    ));
}

我认为您需要为所有这些重新编写代码。不幸的是,我不能给出具体细节,因为我不知道AnnoncesBundle'Form'Model'AddAnnoncesBundle'Entity'CategoriesAnnonce以及它们如何相关或应该如何相互作用。但我怀疑您对创建表单类和创建自定义字段类型以及可能如何创建层次结构以及不同组件应该做什么感到困惑。我将查看本书的表单章节和自定义字段类型说明书条目。然后,如果您遇到问题,请使用实体/模型、窗体和字段类型类中的相关代码创建一个有关细节的新问题。