简体   繁体   English

doctrine 获取 OneToMany 关系结果

[英]doctrine get OneToMany relation results

I have 3 entities Advert , Applications , ApplicationFiles我有 3 个实体AdvertApplicationsApplicationFiles

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();
    }

What I'm trying to do, is when I remove an Advert is to delete every Application with it's related ApplicationsFiles - which I did我想要做的是,当我删除广告时,是删除每个Application相关的ApplicationsFiles - 我做了

My problem is that i can't manage to get the file names for each Advert so i could delete them.我的问题是我无法获取每个Advert的文件名,因此我可以删除它们。

All I could manage to do is to get only one result...我所能做的就是只得到一个结果......

$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()
        ;
}

Dump -倾倒 -

array:1 [▼
  0 => array:2 [▼
0 => Application^ {#1186 ▶}
"name" => "hrm-01-application-5db552d60410c.doc"
 ]
]

Any help would be very appreciated:)任何帮助将不胜感激:)

I think in this case you should query not Applications entity, but ApplicationFiles .我认为在这种情况下,您应该查询的不是Applications实体,而是ApplicationFiles Then you will get array of files to delete.然后你会得到要删除的文件数组。

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

I'm not sure about $post var is it ID or Advert object?我不确定$post var 是ID还是Advert object? If it's object then we can remove second join and use only join with Application entity.如果它是 object 那么我们可以删除第二个连接并仅使用与Application实体连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM