Tôi muốn xây dựng SQL sau đây sử dụng xây dựng truy vấn Học thuyết của:Học thuyết xây dựng truy vấn sử dụng bên trong tham gia với điều kiện
select c.*
from customer c
join phone p
on p.customer_id = c.id
and p.phone = :phone
where c.username = :username
Đầu tiên tôi đã cố gắng
$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');
Nhưng tôi nhận được lỗi sau
Error: expected end of string, got 'ON'
Sau đó, tôi đã cố gắng
$qb->select('c')
->innerJoin('c.phones', 'p')
->where('c.username = :username')
->andWhere('p.phone = :phone');
có vẻ như đang hoạt động. Tuy nhiên, có ai biết điều gì sai với lần thử đầu tiên không? Tôi muốn thực hiện công việc đầu tiên vì nó giống với cách cấu trúc SQL chặt chẽ hơn. Cảm ơn trước!
Lưu ý: Tôi biết chúng tôi cũng có thể viết mysql hoặc dql gốc với Doctrine, nhưng tôi thích trình tạo truy vấn.
EDIT: Dưới đây là toàn bộ mã
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();
}
}
bạn có thể vui lòng cung cấp toàn bộ thông báo lỗi? – hacfi
QueryException: [Lỗi cú pháp] dòng 0, col 74: Lỗi: Dự kiến kết thúc chuỗi, nhận 'ON' –