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.
自从上一个LTS版本的Symfony在几天前发布(30.11.2015),我开始玩它。不幸的是,我不能生成带有与Symfony 2.7.7中正常工作的代码相同的写操作的CRUD。
First I create a new Symfony project using the bash
under Linux Mint 17.2:
首先,我使用Linux Mint 17.2中的bash创建一个新的Symfony项目:
symfony new tasks lts
The new directory tasks
gets created with a new Symfony 2.8.0 project inside.
新目录任务在新Symfony 2.8.0项目中创建。
After adapting the database credentials in app/config/parameters.yml
I create the database:
在应用程序/配置/参数中修改数据库凭据之后。yml I创建数据库:
app/console doctrine:database:create
and generate a new bundle:
并生成一个新的包:
app/console generate:bundle --namespace=Acme/TasksBundle --format=yml
Then I create a new directory src/Acme/TasksBundle/Resources/config/doctrine
and place two files for my models inside. These are:
然后我创建一个新的目录src/Acme/TasksBundle/Resources/config/doctrine,并为我的模型放置两个文件。这些都是:
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
The database schema should like this:
数据库模式应该如下所示:
+----------------+ +--------------+
| task | | task_tag | +---------+
+----------------+ +--------------+ | tag |
| id |<--->| task_id | +---------+
| description | | tag_id |<--->| id |
+----------------+ +--------------+ | name |
+---------+
Now I can generate the entities:
现在我可以生成实体:
app/console generate:doctrine:entities AcmeTasksBundle
This works fine, so the database can be updated:
这很好,所以数据库可以更新:
app/console doctrine:schema:update --force
Everything ok till now. The tables are in the database. Now I want to generate CRUD with write actions:
一切都好,直到现在。表在数据库中。现在我想用写操作生成CRUD:
app/console generate:doctrine:crud --entity=AcmeTasksBundle:Task --with-write --format=yml
After confirming few questions it generates the CRUD and prints out:
在确认了几个问题后,产生了CRUD并打印出来:
Generating the CRUD code: OK
and afterwards throws this error:
然后抛出这个错误
[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.
创建控制器,但不创建窗体。
Generating the CRUD without write options works fine. The very same code works flawlessly with Symfony 2.7.7.
在没有写选项的情况下生成CRUD是可行的。同样的代码在Symfony 2.7.7中可以完美地工作。
I checked the differences in the file form/FormType.php.twig
between the versions and here are the relevant parts:
我检查了文件form/ form .php中的差异。各版本之间的细枝及相关部分如下:
Symfony 2.7.7vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Resources/skeleton/form/FormType.php.twig
Symfony 2.7.7供应商/ sensio赞助/ generator-bundle / sensio赞助/包/ GeneratorBundle /资源/框架/形式/ 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.0vendor/sensio/generator-bundle/Resources/skeleton/form/FormType.php.twig
Symfony 2.8.0供应商/ sensio赞助/ generator-bundle /资源/框架/形式/ 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 %}
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
.)
如我所见,for循环中的if条件是发生错误的地方。(我假定表达式fields_mapping[field]['type']会导致问题,因为许多字段(标记)没有属性类型。)
What I am doing wrong? How can I solve this problem? Thank you very much for your help.
我做错了什么?我如何解决这个问题?非常感谢你的帮助。
EDIT: The same problem occurs with Symfony 3.0.0. The file form/FormType.php.twig
has been changed since version 2.8.
编辑:同样的问题发生在Symfony 3.0.0。文件/ FormType.php形式。自从2.8版本以来,twig已经被改变了。
3 个解决方案
#1
17
Looks like a regression after datetime fix in the generator bundle.
看起来像是在生成器包中的datetime fix之后的回归。
A quick solution is to revert to v2.* in your composer.json
:
一个快速的解决方案是恢复到v2。*在你composer.json:
"sensio/generator-bundle": "^2.5",
The best solution is to fork the repo, fix the bug and create a pull request to contribute back to the community.
最好的解决方案是分叉repo,修复bug,并创建一个拉请求以回馈社区。
Since you already did all the job to isolate the bug, the fix is trivial: check if type
exists in Resources/skeleton/form/FormType.php.twig
. Something like
由于您已经完成了隔离错误的所有工作,所以修复工作是很简单的:检查类型是否存在于Resources/skeleton/form/ form/ form .php.twig中。类似的
{%- if fields_mapping[field]['type'] is defined and fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}
unless the bug masks more hidden errors based on the same assumption.
除非错误掩盖了基于相同假设的更多隐藏错误。
#2
16
I was researching a little bit and tried to debug the error.
我做了一些研究,试图调试错误。
As I mentioned above, the file form/FormType.php.twig
has been changed since the version 2.8.0.
正如我上面提到的,文件form/ form .php。twig自2.8.0版本以来已经被修改。
Obviously the Symfony makers wanted to enhance the forms and automatically resolve the types date
, time
and datetime
. This happens in the line:
显然,Symfony制作者想要增强表单并自动解析类型日期、时间和时间。这发生在这一行:
{%- if fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}
This should be achieved with the help of the array fields_mapping
.
这应该在array 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:
通过一些快速而复杂的工作区,我试图找出fields_mapping中隐藏的内容。这是我的模型的结果:
Task
任务
{
id => {
id => 1,
fieldName => id,
type => integer,
columnName => id
},
description => {
fieldName => description,
type => text,
columnName => description
}
}
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:
在遍历任务字段时,在最后一步中,它将遍历字段标记。if子句中的表达式是这样的:
fields_mapping['tags']['type']
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.
正如我们在前面的示例中看到的,在fields_mapping中没有用于任务的键标记,只有id和description。因为键标签不存在,所以会抛出错误。
I changed the concerned line in the file form/FormType.php.twig
to look like this:
我更改了文件form/ form .php中的相关行。细枝看起来像这样:
{%- 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.
现在我们可以使用这个新特性,通过检查数组中是否存在键来防止错误。
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.
我不知道这是一个bug还是我的问题出了问题。现在,自从2.8.0和3.0.0版本发布以来,已经有一个星期了,所以可能有成千上万的用户在使用它们。我不敢相信,如果是一个bug,没有人会注意到这一点。
EDIT:
编辑:
I posted an issue on GitHub:
我在GitHub上发表了一篇文章:
https://github.com/sensiolabs/SensioGeneratorBundle/issues/443
https://github.com/sensiolabs/SensioGeneratorBundle/issues/443
This was a bug, that has been solved in the same way, as I thought and wrote above:
这是一个bug,已经用同样的方式解决了,就像我上面想和写的那样:
https://github.com/Maff-/SensioGeneratorBundle/commit/205f64e96a94759f795271cb00fc86fb03b1fd4a
https://github.com/Maff-/SensioGeneratorBundle/commit/205f64e96a94759f795271cb00fc86fb03b1fd4a
#3
3
Even if after updating fixed bundle the issue is still existing, sometimes the easiest way to solve the problem is to delete the vendor
catalog, and update composer.
即使在更新固定包之后,问题仍然存在,有时解决问题的最简单方法是删除供应商目录,并更新composer。
#1
17
Looks like a regression after datetime fix in the generator bundle.
看起来像是在生成器包中的datetime fix之后的回归。
A quick solution is to revert to v2.* in your composer.json
:
一个快速的解决方案是恢复到v2。*在你composer.json:
"sensio/generator-bundle": "^2.5",
The best solution is to fork the repo, fix the bug and create a pull request to contribute back to the community.
最好的解决方案是分叉repo,修复bug,并创建一个拉请求以回馈社区。
Since you already did all the job to isolate the bug, the fix is trivial: check if type
exists in Resources/skeleton/form/FormType.php.twig
. Something like
由于您已经完成了隔离错误的所有工作,所以修复工作是很简单的:检查类型是否存在于Resources/skeleton/form/ form/ form .php.twig中。类似的
{%- if fields_mapping[field]['type'] is defined and fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}
unless the bug masks more hidden errors based on the same assumption.
除非错误掩盖了基于相同假设的更多隐藏错误。
#2
16
I was researching a little bit and tried to debug the error.
我做了一些研究,试图调试错误。
As I mentioned above, the file form/FormType.php.twig
has been changed since the version 2.8.0.
正如我上面提到的,文件form/ form .php。twig自2.8.0版本以来已经被修改。
Obviously the Symfony makers wanted to enhance the forms and automatically resolve the types date
, time
and datetime
. This happens in the line:
显然,Symfony制作者想要增强表单并自动解析类型日期、时间和时间。这发生在这一行:
{%- if fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}
This should be achieved with the help of the array fields_mapping
.
这应该在array 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:
通过一些快速而复杂的工作区,我试图找出fields_mapping中隐藏的内容。这是我的模型的结果:
Task
任务
{
id => {
id => 1,
fieldName => id,
type => integer,
columnName => id
},
description => {
fieldName => description,
type => text,
columnName => description
}
}
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:
在遍历任务字段时,在最后一步中,它将遍历字段标记。if子句中的表达式是这样的:
fields_mapping['tags']['type']
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.
正如我们在前面的示例中看到的,在fields_mapping中没有用于任务的键标记,只有id和description。因为键标签不存在,所以会抛出错误。
I changed the concerned line in the file form/FormType.php.twig
to look like this:
我更改了文件form/ form .php中的相关行。细枝看起来像这样:
{%- 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.
现在我们可以使用这个新特性,通过检查数组中是否存在键来防止错误。
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.
我不知道这是一个bug还是我的问题出了问题。现在,自从2.8.0和3.0.0版本发布以来,已经有一个星期了,所以可能有成千上万的用户在使用它们。我不敢相信,如果是一个bug,没有人会注意到这一点。
EDIT:
编辑:
I posted an issue on GitHub:
我在GitHub上发表了一篇文章:
https://github.com/sensiolabs/SensioGeneratorBundle/issues/443
https://github.com/sensiolabs/SensioGeneratorBundle/issues/443
This was a bug, that has been solved in the same way, as I thought and wrote above:
这是一个bug,已经用同样的方式解决了,就像我上面想和写的那样:
https://github.com/Maff-/SensioGeneratorBundle/commit/205f64e96a94759f795271cb00fc86fb03b1fd4a
https://github.com/Maff-/SensioGeneratorBundle/commit/205f64e96a94759f795271cb00fc86fb03b1fd4a
#3
3
Even if after updating fixed bundle the issue is still existing, sometimes the easiest way to solve the problem is to delete the vendor
catalog, and update composer.
即使在更新固定包之后,问题仍然存在,有时解决问题的最简单方法是删除供应商目录,并更新composer。