首先,我不知道如何正确描述我想要的东西,所以这个问题可能不会那么激动
我正在使用Symfony的框架和学说。 我的主要目标是创建一个带有鉴别器列的单个表,并在每次提交上载文件时触发EventListener。
abstract class UploadFile {}
扩展它的三个类
class FanUploadFile extends UploadFile {}
class SupporterUploadFile extends UploadFile {}
class ProductionUploadFile extends UploadFile {}
我的EventListener
class UploadFileListener
{
private $uploadDirs;
private $targetDir;
public function __construct(array $uploadDirs)
{
$this->uploadDirs = $uploadDirs;
$this->targetDir = $uploadDirs['product_file'];
}
public function preUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ((!$entity instanceof Production) &&
(!$entity instanceof Fan) &&
(!$entity instanceof Supporter)) {return;}
// loop over the uploaded files
foreach ($entity->getUploadedFiles() as $uploadedFile) {
// check if the file is to be uploaded
if (!$uploadedFile instanceof UploadedFile)
continue;
$fileName = md5(uniqid()) . '.' .$uploadedFile->guessExtension();
// case to instantiate new object for each case
switch ($entity) {
case $entity instanceof Production:
$pf = new ProductionUploadFile();
break;
case $entity instanceof Fan:
$pf = new FanUploadFile();
break;
case $entity instanceof Supporter:
$pf = new SupportUploadFile();
break;
}
$pf->setPath($fileName)
->setMimeType($uploadedFile->getMimeType())
->setClientName($uploadedFile->getClientOriginalName());
$entity->addFile($pf);
$uploadedFile->move($this->targetDir, $fileName);
}
}
}
此外我的映射(使用orm.xml)
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="AppBundle\Entity\UploadFile" inheritance-type="SINGLE_TABLE">
<discriminator-column name="discr" length="20" />
<discriminator-map>
<discriminator-mapping value="fan" class="FanUploadFile" />
<discriminator-mapping value="support" class="SupportUploadFile" />
<discriminator-mapping value="production" class="ProductionUploadFile" />
</discriminator-map>
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
<field name="path" length="40" />
<field name="mimeType" />
<field name="clientName" />
</entity>
其他课程
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="AppBundle\Entity\SupportUploadFile**" />
</doctrine-mapping>
**所有更改实体的类都相同
目前的错误是:Call to a member function setMimeType() on null
UploadFile
中的访问方法和其他类都是空的。如果我从UploadFile中移除摘要我
Entity 'AppBundle\Entity\UploadFile' has to be part of the discriminator map of 'AppBundle\Entity\UploadFile' to be properly mapped in the inheritance hierarchy.
但我不知道如何将UploadFiles
映射到其他实体Production,Fan,Support
。
基本上我有一个表格foreach表(Production,Fan,Support)
三个实体(FanUploadFile,ProductionUploadFile,SupportUploadFile)
Mappedby:UploadFile
因此,当用户提交表单etc Production
时
我希望通过与UploadFile
的ProductionUploadFile鉴别器映射将文件保存在discr Production
中
但我无法理解如何建立
生产&lt; - &gt; UploadFile&lt; - &gt; ProductionUploadFile