我正在尝试在Doctrine2查询生成器中加入表。这是我加入两个表并得到结果的查询。
最初我有两个表要加入:
- >雇员 - > org_employees
其他表:
- >用户
在我的Doctrine2查询构建器代码中,我已经加入了2个表,通过传递组织ID来获取特定组织员工的结果。
SELECT
*
FROM org_branch_employees oe
LEFT JOIN employees e ON oe.employee_id = e.id
WHERE
oe.org_id = 1;
通过使用查询生成器,上面的sql代码已经改变如下。
$qb = $this->entityManager->createQueryBuilder();
$qb->select('oe', 'e', 'o')
->from('Employee\Entity\OrgEmployee', 'oe')
->leftJoin('oe.organization', 'o')
->leftJoin('oe.employee', 'e')
->where('oe.organization = :organizationId')
->setParameter('organizationId', $orgId);
$query = $qb->getQuery();
$orgEmployees = $query->getResult();
return $orgEmployees;
这是我的员工实体:
<?php
namespace Employee\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Library\Entity\BaseEntity;
use Users\Entity\User;
use Organization\Entity\Organization;
//use Organization\Entity\OrgEmployee;
/**
* Description of Employee
*
* @author Macwin
*/
/**
* @ORM\Entity
* @ORM\Table(name="employees")
*/
class Employee extends BaseEntity {
/**
* @ORM\OneToOne(
* targetEntity="Users\Entity\User"
* )
* @ORM\JoinColumn(
* name="user_id",
* referencedColumnName="id",
* nullable=false
* )
*/
private $user;
/**
* @ORM\Column(name="employee_code", type="string", nullable=true)
* @var string
*/
protected $empCode;
/**
* @ORM\OneToOne(
* targetEntity="Organization\Entity\Organization"
* )
* @ORM\JoinColumn(
* name="org_id",
* referencedColumnName="id",
* nullable=false
* )
*/
private $organization;
/**
* @ORM\OneToMany(targetEntity="Employee\Entity\OrgEmployee", mappedBy="employee")
*/
protected $orgEmployee;
/**
* @ORM\OneToMany(targetEntity="Employee\Entity\OrgEmployeeDesignation", mappedBy="employee")
*/
protected $orgEmployeeDesignation;
public function __construct() {
$this->organizations = new \Doctrine\Common\Collections\ArrayCollection();
parent::__construct();
}
public function getOrganizations() {
return $this->organizations;
}
public function addOrganization(Organization $organization = null) {
$this->organizations->add($organization);
}
public function setUser(User $user = null) {
$this->user = $user;
}
public function getUser() {
return $this->user;
}
public function getEmpCode() {
return $this->empCode;
}
public function setEmpCode($empCode) {
$this->empCode = $empCode;
}
public function setOrganization(Organization $organization = null) {
$this->organization = $organization;
}
public function getOrganization() {
return $this->organization;
}
function getOrgEmployeeDesignation() {
return $this->orgEmployeeDesignation;
}
function setOrgEmployeeDesignation($orgEmployeeDesignation) {
$this->orgEmployeeDesignation = $orgEmployeeDesignation;
}
public function getOrgEmployee() {
return $this->orgEmployee;
}
public function __toString() {
return __CLASS__ . ": [id: {$this->id}, name: {$this->name}]";
}
}
这是我的OrgEmployee实体,它映射了Organization表和Enity表,以获取组织详细信息和员工详细信息。
<?php
namespace Employee\Entity;
use Doctrine\ORM\Mapping as ORM;
use Library\Entity\BaseEntity;
use Employee\Entity\Employee;
use Organization\Entity\Organization;
/**
* Description of Org Employees
*
* @author Macwin
*/
/**
* @ORM\Entity
* @ORM\Table(name="org_branch_employees")
*/
class OrgEmployee extends BaseEntity{
/**
* @ORM\ManyToOne(targetEntity="Employee\Entity\Employee", inversedBy="orgEmployee")
* @ORM\JoinColumn(name="employee_id",referencedColumnName="id",nullable=false)
*/
protected $employee;
/**
* @ORM\ManyToOne(targetEntity="Organization\Entity\Organization", inversedBy="orgEmployee")
* @ORM\JoinColumn(name="org_branch_id", referencedColumnName="id", nullable=false)
*/
protected $organization;
public function setEmployee(Employee $employee = null)
{
$this->employee = $employee;
return $this;
}
public function getEmployee()
{
return $this->employee;
}
public function setOrganization(Organization $organization = null)
{
$this->organization = $organization;
return $this;
}
public function getOrganization()
{
return $this->organization;
}
}
以下是我获取组织详细信息和员工详细信息的方式:
'employeeCode' => $orgEmp->getEmployee()->getEmpCode(),
userFirstName = $orgEmp->getEmployee()->getUser()->getFirstName(),
正在,员工表有用户表的映射,我可以获取用户信息,
到目前为止一直很好,但是当我想在这里加入更多表时,我无法得出确切的结果。
但是当我需要在上述功能中制作滤镜时,我不确定,怎样才能带来精确的结果。
喜欢按employee_code过滤,用户名。
任何人都可以指导我带来结果。我正在REST API方面将结果提供给客户端。分页也在功能中。
如果我正确地说,我正在尝试以下方法:
SELECT
*
FROM org_branch_employees oe
LEFT JOIN employees e ON oe.employee_id = e.id
LEFT JOIN users u ON e.user_id = u.id
WHERE
oe.org_id = 1 AND
u.user_first_name = "John" and
e.employee_code = "EMP777"
答案 0 :(得分:2)
所以第一个查询是否正常工作?您只需要加入用户实体并添加一些条件?或许我误解了这个问题。
$qb = $this->entityManager->createQueryBuilder();
$qb->select('oe', 'e', 'o', 'user')
->from('Employee\Entity\OrgEmployee', 'oe')
->leftJoin('oe.organization', 'o')
->leftJoin('oe.employee', 'e')
->leftJoin('e.user','user')
->andWhere('oe.organization = :organizationId')
->setParameter('organizationId', $orgId),
->andWhere('user.user_first_name = :userFirstName')
->setParameter('userFirstName', 'John'),
->andWhere('e.employee_code = :employeeCode')
->setParameter('employeeCode', 'Emp666');
return $qb->getQuery()->getResult();