0

我正在使用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 子句吗?

4

1 回答 1

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;
}
于 2015-07-03T20:03:38.087 回答