SELECT * FROM PropertyRequest p JOIN leads l WHERE p.lead_id= l.id
如何用doctrine2 querybuilder编写这个sql语句?我正在尝试这个
$qb = $this->createQueryBuilder('p')
->Join('p.lead', 'm')
->Where('m.id = :lead')
->setParameter(':lead', $lead);
return $qb->getQuery()->getResult();
但不起作用
答案 0 :(得分:0)
我怀疑从这里开始,你将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();
如果这不起作用,您需要提供错误消息,也可能是两个实体的定义。
答案 1 :(得分:0)
这是我查询的方式。
// ---控制器
$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 Some\SomeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Some\SomeBundle\Entity\SomeEntity
*
* @ORM\Table(name="sometable")
* @ORM\Entity
*/
class SomeEntity
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=75, nullable=false)
*/
private $name;
/**
* @var SomeOtherEntity
*
* @ORM\ManyToOne(targetEntity="SomeOtherEntity")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="lead", referencedColumnName="onelead")
* })
*/
private $lead;
/**
* Set lead
*
* @param Some\SomeBundle\Entity\SomeOtherEntity $lead
* @return SomeOtherEntity
*/
public function setLead(\Some\SomeBundle\Entity\SomeOtherEntity $lead = null)
{
$this->lead = $lead;
return $this;
}
/**
* Get lead
*
* @return Some\SomeBundle\Entity\SomeOtherEntity
*/
public function getLead()
{
return $this->lead;
}
}
是否会抛出错误?