直接返回结果集:
getConnection反回了vendordoctrinedballibDoctrineDBALDriverConnection.php接口の实现,所以Connectionの所有public方法都可用。
doctrine执行原生sql实例:
<?php
namespace AppController;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentRoutingAnnotationRoute;
use DoctrineORMEntityManagerInterface;
use DoctrineORMQueryResultSetMapping;
use AppUtilsClassUtils;
class ApiCustomController extends AbstractController
{
private $em;
public function __construct(EntityManagerInterface $em){
$this->em = $em;
}
/**
* @Route("/api_custom/{entityName}/groupId/{groupId}/limit/{limit}", methods={"GET"}, name="api_custom")
*/
public function getResourceByGroupId($entityName,$groupId,$limit)
{
$query = $this->em->getConnection()
->query("SELECT * FROM $entityName r WHERE r.resourceGroupId = $groupId ORDER BY r.id LIMIT $limit");
$res = $query->fetchAll();
return $this->json($res);
}
}
返回对象:
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/native-sql.html#native-sql