顾问实体
<?php
namespace Entity;
/**
* @Entity
* @Table(name="consultant")
*/
class Consultant{
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
Paitent Entity
<?php
namespace Entity;
/**
* @Entity
* @Table(name="patient")
*/
class Patient{
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Many Patients belogns to a single consultant.
* @ManyToOne(targetEntity="consultant")
* @JoinColumn(name="consultant_id", referencedColumnName="id", nullable=false)
*/
private $consultant;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set consultant
*
* @param \Entity\consultant $consultant
*
* @return Patient
*/
public function setConsultant(\Entity\consultant $consultant)
{
$this->consultant = $consultant;
$consultant->addPatient($this);
return $this;
}
/**
* Get consultant
*
* @return \Entity\consultant
*/
public function getConsultant()
{
return $this->consultant;
}
}
查询构建器
$qb = $this->em->createQueryBuilder();
$qb
->select('p')
->from('Entity\Consultant', 'c')
->innerJoin('Entity\Patient', 'p', 'c.id = p.consultant_id')
->where('c.id = ?1')
->setParameter(1, 1)
$query = $qb->getQuery();
$results = $query->getResult();
问题
我需要让所有属于特定顾问的患者和我目前在学说2中使用上述查询构建器查询。为了优化上述查询,我是否必须在患者实体中索引consultant_id?如果是,如何在学说2中做到这一点?此外,还欢迎任何其他优化技术。
答案 0 :(得分:0)
Doctrine会自动在外键字段上创建索引,因此无需尝试手动添加索引。同样,为了使所有属于顾问的患者不需要如问题所示,但可以使用下面提到的更简单的查询。
新查询
$qb = $this->em->createQueryBuilder();
$qb
->select('p.name')
->from('Entity\Patient', 'p')
->where('p.consultant = ?1')
->setParameter(1, 1);