我正在尝试执行多对多查询。
这个MySQL查询给了我我想要的东西:
select * from service_offer
left join service on service.id_service = service_offer.id_service
left join user on user.id_user = service_offer.id_user
我如何处理Symfony?
我尝试过类似的事情:
public function findAllServiceOffers(){
$query = $this->getEntityManager()
->createQuery(
'SELECT s, u FROM AppBundle:Service s
INNER JOIN AppBundle:User u'
);
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
但我得到了这个错误:
[语法错误]第0行,第1栏:错误:预期的Doctrine \ ORM \ Query \ Lexer :: T_WITH,得到了字符串的结尾。
如果我添加WHERE或WITH子句,则执行查询,但我有NULLs值...
提前致谢。
答案 0 :(得分:1)
我很快会给你两个例子:
//use Doctrine\DBAL\Connection;
/** @var Connection $conn */
$conn = $this->getDoctrine()->getConnection();
$aResults = $conn->executeQuery($sql)->fetchAll();
OR
//use Doctrine\DBAL\Connection;
/** @var Connection $conn */
$conn = $this->getDoctrine()->getConnection();
$query = $conn->createQueryBuilder()->select('fields')->from('table1','alias_table1');
$query->leftJoin('alias_table1','table2','alias_table2','alias_table1.ID = alias_table2.ID');
$aResults = $query->execute()->fetchAll();
OR
//use Doctrine\ORM\EntityManager;
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
$sSQL = $em->createQuery("SELECT t1,t2 FROM YourBundle:Table1 t1 LEFT JOIN t1.fieldOfEntityDoctrine t2 WHERE t2.ID = 'example'")->getSQL();