[英]Inner join with ProxyQuery + where clause
我正在使用Sonata Admin軟件包 ,但在形成查詢以顯示數據時遇到了麻煩。
我想根據登錄的用戶顯示數據。
在我的數據庫中,有以下表格:
-工作表
- id
- title
- description
- ....
- company_id (FK)
-申請表
- id
- ...
- job_id (FK)
-公司表
- id
- ...
我想根據公司拉所有應用程序(登錄的用戶也已附加到公司)。 所以,我需要與工作表和公司表+,其中公司等於... 內連接 。
在我的ApplicationAdmin類中,我現在有:
public function createQuery($context = 'list') {
$query = parent::createQuery($context);
$user = $this->getConfigurationPool()->getContainer()->get('security.context')->getToken()->getUser();
if($user->hasRole('ROLE_COMPANY'))
{
// I'M STUCK HERE
$query->setParameter('company', $user->getCompany());
}
return $query;
}
有人可以幫我如何與公司建立2個內部聯接以及where子句嗎?
我假設您的應用程序實體與您的工作實體具有多對一關系,而您的工作實體與您的公司實體具有多對一關系,如下所示
公司實體
<?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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.