我有一个Doctrine实体Document,它与实体File有双向oneToMany关系。所以一个文档可以有许多文件实体。以symfony形式添加和删除实体
现在我想创建一个symfony表单,我可以在其中添加和删除文档中的文件。我设置了包含在通过CollectionType中,DocumentType一个文件类型:
//DocumentType.php
$builder->add('files', Type\CollectionType::class, ['entry_type' => FileType::class])
//FileType.php
$builder->add('id', Type\HiddenType::class);
这样,我得到的文件IDS隐藏字段。我现在想通过JS禁用字段,如果文件应该从文档中删除。但我不能送的形式,因为我得到的错误:
Could not determine access type for property "id".
它只是因为我想利用现场的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
感谢,但我怎么能删除实体呢?当我不添加任何字段来更改其值时,我如何标记实体应该删除? – Asara
是的,您应该添加一些字段,例如我的示例abow来呈现您的条目。您只需使用javascript将其删除,Symfony表单将为您删除该条目。看看这个文档,它非常详细地介绍Symfony表单集合:[如何嵌入一个表单集合](http://symfony.com/doc/current/form/form_collections.html) –
我刚刚尝试过,添加了一个字段,通过JS禁用它,检查它不在POST数据中,但是symfony没有删除它。好的,我会再次检查你的文档页面。 – Asara