mi piacerebbe costruire la seguente SQL utilizzando query builder di Doctrine:Dottrina generatore di query utilizzando inner join con condizioni
select c.*
from customer c
join phone p
on p.customer_id = c.id
and p.phone = :phone
where c.username = :username
Per prima cosa ho provato
$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');
ma sto ottenendo il seguente errore
Error: expected end of string, got 'ON'
Poi ho provato
$qb->select('c')
->innerJoin('c.phones', 'p')
->where('c.username = :username')
->andWhere('p.phone = :phone');
che sembra funzionare. Tuttavia, qualcuno sa cosa c'è che non va nel primo tentativo? Mi piacerebbe fare il primo lavoro dal momento che assomiglia più strettamente a come è strutturato SQL. Grazie in anticipo!
Nota: so che possiamo anche scrivere mysql nativo o dql con Doctrine, ma preferirei il generatore di query.
EDIT: Di seguito è riportato l'intero codice
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();
}
}
Si può fornire l'intero messaggio di errore? – hacfi
QueryException: [Errore di sintassi] riga 0, colonna 74: Errore: fine della stringa prevista, ottenuto "ON" –