我有一個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