我假设您的 Application 实体与您的 Job 实体具有多对一关系,并且您的 Job 实体与您的 Company 实体具有多对一关系,如下所示
公司实体
<?php
use Doctrine\Common\Collections\ArrayCollection;
/** @Entity **/
class Company
{
// ...
/**
* @OneToMany(targetEntity="Job", mappedBy="company")
**/
private $jobs;
// ...
public function __construct() {
$this->jobs= new ArrayCollection();
}
// getter and setters
}
工作实体
/** @Entity **/
class Job
{
// ...
/**
* @ManyToOne(targetEntity="Company", inversedBy="jobs")
* @JoinColumn(name="company_id", referencedColumnName="id")
**/
private $company;
// ...
// ...
/**
* @OneToMany(targetEntity="Application", mappedBy="job")
**/
private $applications;
// ...
public function __construct() {
$this->applications= new ArrayCollection();
}
// getter and setters
}
应用实体
/** @Entity **/
class Application
{
// ...
/**
* @ManyToOne(targetEntity="Job", inversedBy="applications")
* @JoinColumn(name="job_id", referencedColumnName="id")
**/
private $job;
// ...
// getter and setters
}
然后在您的ApplicationAdmin
类的createQuery
函数中,您已经Application
在查询对象中有实体,您可以将其与第一个Job
实体连接,然后与Company
实体连接
public function createQuery($context = 'list') {
$query = parent::createQuery($context);
$user = $this->getConfigurationPool()->getContainer()->get('security.context')->getToken()->getUser();
if($user->hasRole('ROLE_COMPANY'))
{
$query->innerJoin($query->getRootAlias().'.job','j')
->innerJoin('j.company','c')
->where('c.id = :company')
->setParameter('company', $user->getCompany()->getId());
}
return $query;
}