简体   繁体   中英

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.

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:

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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