使用Symfony 2.8生成表单会抛出Twig_Error_Runtime [英] Generating forms with Symfony 2.8 throws a Twig_Error_Runtime

查看:488
本文介绍了使用Symfony 2.8生成表单会抛出Twig_Error_Runtime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Symfony的最后一个LTS版本几天前发布(30.11.2015),所以我开始玩了。不幸的是,我不能使用与Symfony 2.7.7中相同代码一样的编写动作来生成CRUD。

Since the last LTS version of Symfony was released few days ago (30.11.2015) I started playing with it. Unfortunately I can't generate a CRUD with write actions with the same code that works fine in Symfony 2.7.7.

首先,我使用 bash Linux Mint 17.2:

First I create a new Symfony project using the bash under Linux Mint 17.2:

symfony new tasks lts

使用新的Symfony 2.8.0创建新目录任务

The new directory tasks gets created with a new Symfony 2.8.0 project inside.

app / config / parameters.yml 中修改数据库凭据后,我创建数据库: / p>

After adapting the database credentials in app/config/parameters.yml I create the database:

app/console doctrine:database:create

并生成一个新的包:

app/console generate:bundle --namespace=Acme/TasksBundle --format=yml

然后我创建一个新目录 src / Acme / TasksBundle / Resources / config / doctrine 并为我的模型放置两个文件。这些是:

Then I create a new directory src/Acme/TasksBundle/Resources/config/doctrine and place two files for my models inside. These are:

Task.orm.yml

Task.orm.yml

Acme\TasksBundle\Entity\Task:
    type: entity
    repositoryClass: Acme\TasksBundle\Repository\TaskRepository
    table: task
    id:
        id:
            type: integer
            generator: { strategy : AUTO }
    fields:
        description:
            type: text
    manyToMany:
        tags:
            targetEntity: Tag
            inversedBy: tasks
            cascade: [ "persist" ]
            joinTable:
                name: task_tag
                joinColumns:
                    task_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    tag_id:
                        referencedColumnName: id

Tag.orm.yml

Tag.orm.yml

Acme\TasksBundle\Entity\Tag:
    type: entity
    repositoryClass: Acme\TasksBundle\Repository\TagRepository
    table: tag
    id:
        id:
            type: integer
            generator: { strategy : AUTO }
    fields:
        name:
            type: string
            length: 50
    manyToMany:
        tasks:
            targetEntity: Task
            mappedBy: tags

数据库模式应该如下所示: / p>

The database schema should like this:

+----------------+     +--------------+
| task           |     | task_tag     |     +---------+
+----------------+     +--------------+     | tag     |
|   id           |<--->|   task_id    |     +---------+
|   description  |     |   tag_id     |<--->|   id    |
+----------------+     +--------------+     |   name  |
                                            +---------+

现在我可以生成实体:

app/console generate:doctrine:entities AcmeTasksBundle

这个工作正常,所以可以更新数据库:

This works fine, so the database can be updated:

app/console doctrine:schema:update --force

这些表位于数据库中。现在我想用写入动作生成CRUD:

Everything ok till now. The tables are in the database. Now I want to generate CRUD with write actions:

app/console generate:doctrine:crud --entity=AcmeTasksBundle:Task --with-write --format=yml

确认几个问题后,它会生成CRUD并打印出来:

After confirming few questions it generates the CRUD and prints out:

Generating the CRUD code: OK

,然后抛出此错误:

[Twig_Error_Runtime]                                                                                    
Key "tags" for array with keys "id, description" does not exist in "form/FormType.php.twig" at line 29

控制器被创建,但不是窗体。

The controller gets created, but not the form.

生成没有写入选项的CRUD可以正常工作。我用相同的代码与Symfony 2.7.7完美无瑕。

Generating the CRUD without write options works fine. The very same code works flawlessly with Symfony 2.7.7.

我检查了文件 form / FormType.php.twig 之间的版本是相关的部分:

I checked the differences in the file form/FormType.php.twig between the versions and here are the relevant parts:

Symfony 2.7.7

vendor / sensio / generator-bundle / Sensio / Bundle / GeneratorBundle / Resources / skeleton / form / FormType.php.twig

Symfony 2.7.7
vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Resources/skeleton/form/FormType.php.twig

{%- if fields|length > 0 %}
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    {%- for field in fields %}

        ->add('{{ field }}')
    {%- endfor %}

    ;
}
{% endif %}

Symfony 2.8.0

vendor / sensio / generator-bundle / Resources / skeleton / form / FormType.php.twig

Symfony 2.8.0
vendor/sensio/generator-bundle/Resources/skeleton/form/FormType.php.twig

{%- if fields|length > 0 %}
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder

    {%- for field in fields -%}
        {%- if fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}

        ->add('{{ field }}', '{{ fields_mapping[field]['type'] }}')

        {%- else %}

        ->add('{{ field }}')

        {%- endif -%}
    {%- endfor %}

    ;
}
{% endif %}

当我看到if条件for循环是发生错误的地方。 (我假设表达式 fields_mapping [field] ['type'] 导致了这个问题,因为多个到多个字段(标签)没有属性 type 。)

As I see the if condition in the for loop is the place where the error occurs. (I assume that the expression fields_mapping[field]['type'] causes the problem since the many to many field (tag) has no attribute type.)

我做错了什么?如何解决这个问题?非常感谢您的帮助。

What I am doing wrong? How can I solve this problem? Thank you very much for your help.

编辑:
Symfony 3.0.0发生同样的问题。文件 form / FormType.php.twig 自2.8版以来已更改。

The same problem occurs with Symfony 3.0.0. The file form/FormType.php.twig has been changed since version 2.8.

推荐答案

我正在研究一下,并尝试调试错误。

I was researching a little bit and tried to debug the error.

如上所述,文件 form / FormType.php .twig 自版本2.8.0以来已被更改。

As I mentioned above, the file form/FormType.php.twig has been changed since the version 2.8.0.

显然,Symfony制造商希望增强表单并自动解决类型 date time datetime 。这将发生在以下行:

Obviously the Symfony makers wanted to enhance the forms and automatically resolve the types date, time and datetime. This happens in the line:

{%- if fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}

这应该是通过数组 fields_mapping

使用一些快速肮脏的解决方法,我试图找出隐藏在 fields_mapping 。这是我的模型的结果:

With some quick-and-dirty workarounds I tried to find out what is hidden inside of fields_mapping. This is the result for my model:

任务

{
    id => {
        id => 1,
        fieldName => id,
        type => integer,
        columnName => id
    },
    description => {
        fieldName => description,
        type => text,
        columnName => description
    }
}

当迭代任务的字段时,最后一步通过字段标签。 if子句中的表达式如下所示:

When iterating through the fields of Task, in the last step it goes through the field tags. The expression in the if clause looks like this:

fields_mapping['tags']['type']

正如我们在上一个例子中看到的,没有密钥标签在任务的 fields_mapping 中,只有 id 描述。由于密钥标签不存在,所以会抛出错误。

As we see in the previous example, there is no key tags in the fields_mapping for Task, only id and description. Since the key tags doesn't exist, the error is thrown.

我更改了文件中的有关行 form / FormType.php.twig 如下所示:

I changed the concerned line in the file form/FormType.php.twig to look like this:

{%- if fields_mapping[field] is defined and fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}

现在我们可以使用新功能,我们通过检查数组中是否存在密钥来防止错误。

Now we can use the new feature and we prevent an error by checking if the key exists in the array.

我不知道这是否是一个错误,或者我的具体情况有问题。现在自版本2.8.0和3.0.0发布已经有一个星期了,所以可能有几千个用户一直在玩。我不敢相信,如果这是一个错误,没有人会注意到这一点。

I don't know if this is a bug or there is something wrong in my particular case. Now it is already one week since the release of the versions 2.8.0 and 3.0.0, so probably many thousands users have been playing around with them. I couldn't believe that, if it is a bug, nobody would have noticed this.

编辑:

我在GitHub上发布了一个问题:

I posted an issue on GitHub:

https:// github .com / sensioabs / SensioGeneratorBundle / issues / 443

这是一个错误,已经以同样的方式解决,正如我以前想到并写的:

This was a bug, that has been solved in the same way, as I thought and wrote above:

https://github.com/Maff-/SensioGeneratorBundle / commit / 205f64e96a94759f795271cb00fc86fb03b1fd4a

这篇关于使用Symfony 2.8生成表单会抛出Twig_Error_Runtime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆