0
顧問實體索引外鍵優化查詢
<?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實體
<?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中做到這一點?還有其他任何優化技術也受到歡迎。