所以,这就是我现在面临的问题。我试图从一个表中进行简单的简单选择,并且出于某种原因,Doctrine将自引用连接添加到SQL查询中。我很确定它不会影响查询速度,但我还是希望摆脱它。
以下是我构建查询的方式:
$qb = $this->createQueryBuilder('ol');
$qb->select('o')
->from('ApplicationPersonBundle:Occupation', 'o')
->where($qb->expr()->eq('o.person', '?1'))
->addOrderBy('o.toYear', 'DESC')
->addOrderBy('o.toMonth', 'DESC')
;
$qb->setParameter(1, $person);
这就是Doctrine所产生的:
SELECT
o0_.id AS id0,
o0_.description AS description1,
o0_.from_month AS from_month2,
o0_.from_year AS from_year3,
o0_.to_month AS to_month4,
o0_.to_year AS to_year5,
o0_.person_id AS person_id6,
o0_.company_id AS company_id7,
o0_.position_id AS position_id8
FROM
occupation o1_,
occupation o0_
WHERE
o0_.person_id = ?
ORDER BY
o0_.to_year DESC,
o0_.to_month DESC
我不知道在哪里" FROM职业o1_,职业o0 _"来自。它甚至从未使用过那个o1_别名。
实体的有意义的部分看起来像这样,没有自我引用或诸如此类的东西:
/**
* Occupation
*
* @ORM\Table(name="occupation")
* @ORM\Entity(repositoryClass="Application\Bundle\PersonBundle\Entity\OccupationRepository")
*/
class Occupation
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Person
*
* @ORM\OneToOne(targetEntity="Person")
* @ORM\JoinColumn(name="person_id", referencedColumnName="id")
*/
protected $person;
}
答案 0 :(得分:3)
你从职业存储库里面获取qb是否正确?无需为占用实体明确添加select或from。所以这样的事情应该有效:
$qb = $this->createQueryBuilder('o'); // Change from o1, this is the occupation alias
$qb->
->where($qb->expr()->eq('o.person', '?1'))
->addOrderBy('o.toYear', 'DESC')
->addOrderBy('o.toMonth', 'DESC')
;
$qb->setParameter(1, $person);
答案 1 :(得分:0)
你能告诉我们第二个实体吗? 也许它还有一个JoinColumn注释?