【问题标题】:Inner join with ProxyQuery + where clause使用 ProxyQuery + where 子句进行内连接
【发布时间】:2015-09-19 18:20:50
【问题描述】:

我正在使用Sonata Admin bundle,但在形成查询以显示数据时遇到问题。

我想根据登录的用户显示数据。
在我的数据库中,我有以下表格:


- 工作表

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


- 申请表

 - id
 - ...
 - job_id (FK)


- 公司表

 - id
 - ...

我想根据公司提取所有应用程序(登录的用户也附属于公司)。所以我需要一个 inner join 与工作表和公司表 + 哪里公司等于...。

在我的 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 mysql symfony sonata-admin sonata


    【解决方案1】:

    我假设您的 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多