自从几天前(30.11.2015)发布了 Symfony 的最后一个 LTS 版本以来,我开始使用它。不幸的是,我无法使用在 Symfony 2.7.7 中正常工作的相同代码生成具有写入操作的 CRUD。
bash
首先,我使用Linux Mint 17.2创建一个新的 Symfony 项目:
symfony new tasks lts
新目录tasks
被创建,里面有一个新的 Symfony 2.8.0 项目。
在app/config/parameters.yml
我创建数据库中调整数据库凭据后:
app/console doctrine:database:create
并生成一个新包:
app/console generate:bundle --namespace=Acme/TasksBundle --format=yml
然后我创建一个新目录src/Acme/TasksBundle/Resources/config/doctrine
并在其中放置我的模型的两个文件。这些是:
任务.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
标签.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
数据库模式应该是这样的:
+----------------+ +--------------+
| task | | task_tag | +---------+
+----------------+ +--------------+ | tag |
| id |<--->| task_id | +---------+
| description | | tag_id |<--->| id |
+----------------+ +--------------+ | name |
+---------+
现在我可以生成实体:
app/console generate:doctrine:entities AcmeTasksBundle
这工作正常,因此可以更新数据库:
app/console doctrine:schema:update --force
到目前为止一切正常。这些表在数据库中。现在我想用写操作生成 CRUD:
app/console generate:doctrine:crud --entity=AcmeTasksBundle:Task --with-write --format=yml
在确认几个问题后,它会生成 CRUD 并打印出来:
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
控制器被创建,但不是表单。
在没有写入选项的情况下生成 CRUD 可以正常工作。相同的代码在 Symfony 2.7.7 中完美运行。
我检查了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
{%- 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 %}
正如我所见,for 循环中的 if 条件是发生错误的地方。(我假设表达式fields_mapping[field]['type']
会导致问题,因为多对多字段 ( tag
) 没有属性type
。)
我做错了什么?我怎么解决这个问题?非常感谢您的帮助。
编辑:Symfony 3.0.0 也会出现同样的问题。该文件form/FormType.php.twig
自 2.8 版以来已更改。