加入,我想使用Doctrine的查询生成器来构造SQL语句:主义查询生成器与条件
select c.*
from customer c
join phone p
on p.customer_id = c.id
and p.phone = :phone
where c.username = :username
首先我想
$qb->select('c')
->innerJoin('c.phones', 'p', Join::ON, $qb->expr()->andx(
$qb->expr()->eq('p.customerId', 'c.id'),
$qb->expr()->eq('p.phone', ':phone')
))
->where('c.username = :username');
但我发现了以下错误
Error: expected end of string, got 'ON'
然后我试图
$qb->select('c')
->innerJoin('c.phones', 'p')
->where('c.username = :username')
->andWhere('p.phone = :phone');
这似乎是工作。但是,有没有人知道第一次尝试有什么问题?我想做第一个工作,因为它更接近SQL的结构。提前致谢!
注意:我知道我们也可以用Doctrine编写native mysql或dql,但我更喜欢查询构建器。
编辑:下面是整个代码
namespace Cyan\CustomerBundle\Repository;
use Cyan\CustomerBundle\Entity\Customer;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;
class CustomerRepository extends EntityRepository
{
public function findCustomerByPhone($username, $phone)
{
$qb = $this->createQueryBuilder('c');
$qb->select('c')
->innerJoin('c.phones', 'p', Join::ON, $qb->expr()->andx(
$qb->expr()->eq('p.customerId', 'c.id'),
$qb->expr()->eq('p.phone', ':phone')
))
->where('c.username = :username');
// $qb->select('c')
// ->innerJoin('c.phones', 'p')
// ->where('c.username = :username')
// ->andWhere('p.phone = :phone');
$qb->setParameters(array(
'username' => $username,
'phone' => $phone->getPhone(),
));
$query = $qb->getQuery();
return $query->getResult();
}
}
能否请你提供整个以下错误消息的作品? – hacfi 2013-03-13 07:27:07
QueryException:[语法错误]第0行,第74列:错误:字符串的预期结束,'ON' – 2013-03-13 08:50:56