doctrine执行原生sql并直接返回结果集

直接返回结果集:

getConnection反回了\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\Connection.php接口的实现,所以Connection的所有public方法都可用。

doctrine执行原生sql实例:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\ResultSetMapping;

use App\Utils\ClassUtils;

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

猜你喜欢

转载自blog.csdn.net/qq_36110571/article/details/107974977