【问题标题】:doctrine get OneToMany relation results学说获得 OneToMany 关系结果
【发布时间】: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']); }但是,老实说,你应该看看教义事件(删除在应用程序文件实体上可以触发文件系统删除)和级联删除从广告到应用程序到应用程序文件的关系。甚至可能是孤儿移除。

标签: php sql symfony doctrine


【解决方案1】:

我认为在这种情况下你不应该查询Applications实体,而是ApplicationFiles!然后你会得到要删除的文件数组。

$filesToDelete = $m->getRepository(ApplicationsFiles::class)
                   ->createQueryBuilder('af')
                   ->innerJoin('af.application', 'a')
                   ->innerJoin('a.advert','advert')
                   ->andWhere('advert.id = :s')
                   ->setParameter('s', $post)
                   ->getQuery()
                   ->getReqult();

我不确定$post var 是ID 还是Advert 对象?如果它是对象,那么我们可以删除第二个连接并仅使用 Application 实体的连接。

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    相关资源
    最近更新 更多