【发布时间】:2017-04-05 15:16:30
【问题描述】:
我有一个 Doctrine 实体文档,它与实体文件具有双向 oneToMany 关系。所以一个 Document 可以有多个 File 实体。
现在我想制作一个 symfony 表单,我可以在其中添加和删除文档中的文件。我通过 CollectionType 设置了一个包含在 DocumentType 中的 FileType:
//DocumentType.php
$builder->add('files', Type\CollectionType::class, ['entry_type' => FileType::class])
//FileType.php
$builder->add('id', Type\HiddenType::class);
这样我就得到了带有文件 ID 的隐藏字段。如果应该从 Document 中删除文件,我现在想通过 JS 禁用字段。但我无法发送表单,因为我收到错误消息:
Could not determine access type for property "id".
只是因为我想使用Field的id。当然,我可以使用 src 或 File 的任何其他列来识别要删除的正确实体。
但我希望,在 symfony 中有一个整体更好的方法来处理这个问题?
这里是我的实体映射:
AppBundle\Entity\File:
type: entity
table: files
repositoryClass: AppBundle\Repository\FileRepository
manyToOne:
document:
targetEntity: Document
inversedBy: files
joinColumn:
onDelete: CASCADE
AppBundle\Entity\Document:
type: entity
table: documents
repositoryClass: AppBundle\Repository\DocumentRepository
oneToMany:
files:
targetEntity: File
mappedBy: document
【问题讨论】:
标签: php forms symfony doctrine-orm