[英]TYPO3 8 with extbase: remove FileReference
This seems simple enough but I seem to be too stupid to do it. 这似乎很简单,但是我似乎太愚蠢了。 I have added a field to fe_users called "cv" for uploading a pdf file.
我在fe_users中添加了一个名为“ cv”的字段,用于上传pdf文件。 In my FrontendUser Model it looks like this:
在我的FrontendUser模型中,它看起来像这样:
/**
* @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
* @cascade remove
*/
protected $cv;
Uploading a file works like a charm, I used the script from https://github.com/helhum/upload_example in parts. 上载文件的过程就像一个咒语,我部分使用了https://github.com/helhum/upload_example中的脚本。
The only problem is deleting such a file, or rather, removing the connection between sys_file and the user. 唯一的问题是删除此类文件,或者删除sys_file与用户之间的连接。 After submitting a form and checking a checkbox, I tried to do this:
提交表单并选中复选框后,我尝试执行以下操作:
$user->setCv(null);
$user->setEdited(new \DateTime());
$this->frontendUserRepository->update($user);
$persistenceManager->persistAll();
After a page reload if I take a look at the backend, the cv File is still attached to the user (but "edited" was correctly set to the current datetime). 重新加载页面后,如果我查看后端,则cv文件仍将附加到用户(但“已编辑”已正确设置为当前日期时间)。 I do not understand this, how can I set the FileReference Value to null?
我不明白,如何将FileReference值设置为null?
Make sure to check your Model. 确保检查您的模型。
FileReferences
are stored in an M:N relation. FileReferences
以M:N关系存储。 So you must use the folling declaration in your Model: 因此,您必须在模型中使用以下声明:
/**
* cv
*
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
* @cascade remove
*/
protected $cv = null;
with an initialization in the __constructor
: 在
__constructor
进行初始化:
public function __construct()
{
$this->cv = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}
Now you have your FileReference
in an ObjectStorage
. 现在,在
ObjectStorage
有FileReference
。 To clear all FileReferences
you can set cv
to new \\TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage()
: 要清除所有
FileReferences
,可以将cv
设置为new \\TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage()
:
$user->removeCv(new \TYPO3\CMS\Extbase\Persistence\ObjectStorage());
$user->setEdited(new \DateTime());
$this->frontendUserRepository->update($user);
$persistenceManager->persistAll();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.