【发布时间】:2019-10-27 08:21:21
【问题描述】:
我有 3 个实体 Advert、Applications、ApplicationFiles
Advert
/**
* @ORM\OneToMany(targetEntity="App\Entity\Application", mappedBy="advert", orphanRemoval=true)
*/
private $applications;
Applications
/**
* @ORM\OneToMany(targetEntity="App\Entity\ApplicationFiles", mappedBy="app", orphanRemoval=true)
*/
private $applicationFiles;
ApplicationsFiles
public function getFilePath(): string
{
return Uploader::APPLICATION_FILES.'/'.$this->getName();
}
我想要做的是,当我删除广告时,删除每个 Application 及其相关的 ApplicationsFiles - 我做了
我的问题是我无法获取每个Advert 的文件名,所以我可以删除它们。
我所能做的就是只得到一个结果......
$appRepo = $m->getRepository(Application::class);
$rrr = $appRepo->getFilePath($post);
dd($rrr);
getFilePath
/**
* @param $s
* @return mixed
*/
public function getFilePath($s)
{
return $this->createQueryBuilder('a')
->andWhere('advert.id = :s')
->leftJoin('a.applicationFiles','applicationFiles')
->leftJoin('a.advert','advert')
->addSelect('applicationFiles.name')
->setParameter('s', $s)
->getQuery()
->getResult()
;
}
转储 -
array:1 [▼
0 => array:2 [▼
0 => Application^ {#1186 ▶}
"name" => "hrm-01-application-5db552d60410c.doc"
]
]
任何帮助将不胜感激:)
【问题讨论】:
-
仓库的
createQueryBuilder($alias)默认调用->select('EntityClass '.$alias)和->from('EntityClass '.$alias)。现在如果你,而不是addSelect只是调用select(覆盖第一个隐式选择),你应该得到一个文件名数组。不过我可能错了,在这种情况下,请通过以下方式创建一个干净的查询生成器:$this->getEntityManager()->createQueryBuilder()->from(Application:.class, 'a')... -
select有效,我得到了所有结果,现在我无法将implode输出到$this->filesystem->delete(self::APPLICATION_FILES.'/'.$file); -
你会内爆什么? oO
-
这是我从
$appRepo->getFilePath($post);array:3 [▼ 0 => array:1 [▼ "name" => "hrm-01-application-5db552d60410c.doc" ] 1 => array:1 [▼ "name" => "fac-em-5db68af84036f.pdf" ] 2 => array:1 [▼ "name" => "27-sonde-3e-5db68afe97137.xls" ] ]得到的数组,我正在尝试将它发送到$this->filesystem->delete(self::APPLICATION_FILES.'/'.$file);返回布尔值 -
我的意思是......是的,你得到一个带有键'name'的数组所以......
foreach($appRepo->getFilePath($post) as $arr) { $this->filesystem->delete(...$arr['name']); }但是,老实说,你应该看看教义事件(删除在应用程序文件实体上可以触发文件系统删除)和级联删除从广告到应用程序到应用程序文件的关系。甚至可能是孤儿移除。