SELECT * FROM PropertyRequest p JOIN leads l WHERE p.lead_id= l.id
如何使用 doctrine2 查询生成器编写此 sql 语句?我正在尝试这个
$qb = $this->createQueryBuilder('p')
->Join('p.lead', 'm')
->Where('m.id = :lead')
->setParameter(':lead', $lead);
return $qb->getQuery()->getResult();
但不起作用
我怀疑从这里开始,您将lead_id定义为实体 p 中的"铅"。我的回答是基于这些假设。
然后,可以使用此语句执行联接
$qb = $this->createQueryBuilder('p')
->innerJoin('p.lead', 'm', 'WITH', 'p.lead = m.id')
->Where('m.id = :lead')
->setParameter(':lead', $lead);
return $qb->getQuery()->getResult();
如果这不起作用,您需要提供错误消息,可能还需要两个实体的定义。
这就是
我进行查询的方式。
//---控制器
$em = $this->getDoctrine()->getRepository('SomeBundle:SomeEntity');
$query = $em->createQueryBuilder('p')
->select("p")
->leftJoin("p.lead", "c")
->where("m.id = :lead")
->setParameter(":lead", $lead);
return $query->getResult();
//---实体
<?php
namespace SomeSomeBundleEntity;
use DoctrineORMMapping as ORM;
/**
* SomeSomeBundleEntitySomeEntity
*
* @ORMTable(name="sometable")
* @ORMEntity
*/
class SomeEntity
{
/**
* @var integer $id
*
* @ORMColumn(name="id", type="bigint", nullable=false)
* @ORMId
*/
private $id;
/**
* @var string $name
*
* @ORMColumn(name="name", type="string", length=75, nullable=false)
*/
private $name;
/**
* @var SomeOtherEntity
*
* @ORMManyToOne(targetEntity="SomeOtherEntity")
* @ORMJoinColumns({
* @ORMJoinColumn(name="lead", referencedColumnName="onelead")
* })
*/
private $lead;
/**
* Set lead
*
* @param SomeSomeBundleEntitySomeOtherEntity $lead
* @return SomeOtherEntity
*/
public function setLead(SomeSomeBundleEntitySomeOtherEntity $lead = null)
{
$this->lead = $lead;
return $this;
}
/**
* Get lead
*
* @return SomeSomeBundleEntitySomeOtherEntity
*/
public function getLead()
{
return $this->lead;
}
}
它会抛出错误吗?