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

205次阅读
没有评论

直接返回结果集:

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

 

facingscreen
版权声明:本站原创文章,由 facingscreen2022-08-12发表,共计927字。
转载说明:本文为搜栈网原创文章,除特殊说明外皆由CC-4.0协议发布,转载请注明出处,如有帮助欢迎打赏。
评论(没有评论)
验证码