【问题标题】:Symfony 3 Form Entity typeSymfony 3 表单实体类型
【发布时间】:2016-06-26 15:36:54
【问题描述】:

我有一个表单类作为(服务),我使用entity type 选择一篇文章。

在 Symfony 2 中这有效:

$builder
    ->add('article', EntityType::class,
        array(
        'em' => $this->em,
        'class' => 'MyBundle\Entity\Article',
            'query_builder' => function ($em) {
                return $em->createQueryBuilder('a')
                ->where('a.active = 1');
            },
        'choice_label' => 'name')
    )

但在 Symfony3 中出现错误: Neither the property "article" nor one of the methods ...

我的问题是文章没有属性article。当我将文章重命名为名称时

->add('name', ...`

请问如何将文章实体传递给表单?

【问题讨论】:

  • 向我们展示连接到该表单的实体。
  • 您希望使用该字段填充的数据是什么? ($form->getData() 的结果)

标签: php forms class symfony entity


【解决方案1】:

在 Symfony 3 中,您需要使用 Fully Qualified Class Name,所以不要:

'class' => 'MyBundle\Entity\Article',

你需要使用:

'class' => \MyBundle\Entity\Article::class,

在书中了解更多信息:http://symfony.com/doc/current/book/forms.html#building-the-form 也许您想检查从 Symfony 2 升级到 Symfony 3 时需要更改的所有其他内容:https://github.com/symfony/symfony/blob/master/UPGRADE-3.0.md

【讨论】:

    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 2017-05-14
    • 2015-11-01
    • 1970-01-01
    相关资源
    最近更新 更多