简体   繁体   English

内联与ProxyQuery + where子句

[英]Inner join with ProxyQuery + where clause

I'm using the Sonata Admin bundle and I'm having trouble with forming a query to show data. 我正在使用Sonata Admin软件包 ,但在形成查询以显示数据时遇到了麻烦。

I would like to show data depending on the user who's logged in. 我想根据登录的用户显示数据。
In my database I have the following tables: 在我的数据库中,有以下表格:


- Job table -工作表

 - id
 - title
 - description
 - ....
 - company_id (FK)


- Application table -申请表

 - id
 - ...
 - job_id (FK)


- Company table -公司表

 - id
 - ...

I would like to pull all the applications depending on the company (user logged in is also attached to a company). 我想根据公司拉所有应用程序(登录的用户也已附加到公司)。 So I will need an inner join with job table and company table + where company is equal to ... . 所以,我需要与工作表和公司表+,其中公司等于... 内连接

In my ApplicationAdmin class I have now: 在我的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;
}

Can someone help me how I can make 2 inner joins and where clause with company? 有人可以帮我如何与公司建立2个内部联接以及where子句吗?

I assume your Application entity has a many-to-one relation to your Job entity and your job entity has a many-to-one relation with your Company entity something like below 我假设您的应用程序实体与您的工作实体具有多对一关系,而您的工作实体与您的公司实体具有多对一关系,如下所示

Company Entity 公司实体

<?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
}

Job Entity 工作实体

/** @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
}

Application Entity 应用实体

/** @Entity **/
class Application
{
    // ...
    /**
     * @ManyToOne(targetEntity="Job", inversedBy="applications")
     * @JoinColumn(name="job_id", referencedColumnName="id")
     **/
    private $job;
    // ...
    // getter and setters
}

Then in your ApplicationAdmin class's createQuery function you already have Application entity in query object you can join it with first Job entity then with Company entity 然后,在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.

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