- 我有一個基于 api-platform 的簡單 symfony 專案。
- 我有一個名為“MyEntity”的物體。
- 我有一個資源 yaml 組態檔來告訴 api-platform 如何通過 api call 查詢我的物體。
- 在 yaml 中,我添加了一個名為
exportcsv暴露為的路由export,它將由我的前端使用此 url 呼叫:http://127.0.0.1:8000/api/myentitys/export。 - 此路由映射為從 MyEntity 控制器呼叫匯出方法。
- 在 MyEntity 控制器中,我有一個名為的方法
export,除了轉儲一個句子然后死(dd('why?!');)之外,我什么也不做。
預期行為:
- 呼叫匯出 url
- 服務器/資料庫上什么都不應該做,前端只會收到字串的轉儲
why?!
實際行為:
- 呼叫匯出 url
- 在名為 myentity 的 table'db 上執行查詢
- 然后接收字串的轉儲
why?!
當我向表中添加資料時,我發現了這個查詢。隨著我在表格上添加更多資料,性能變得越來越長。Why?!在某些時候我永遠不會達到。我檢查了我的資料庫,發現 myentity 表上的 select all 處于活動狀態。我在檔案上搜索了一下,我唯一能找到的是:
pagination_partial: true
在 yaml 中將其添加到export路由時,它仍會執行查詢,但由于它現在分頁,因此需要的時間更少。
我的問題是:
如何完全洗掉此查詢?
在 Controller/MyEntityController.php 中:
namespace App\Controller;
use App\Entity\MyEntity;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
// use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
class MyEntityController// extends AbstractController
{
private ParameterBagInterface $params;
private EntityManagerInterface $entityManager;
public function __construct(ParameterBagInterface $params, EntityManagerInterface $entityManager)
{
$this->params = $params;
$this->entityManager = $entityManager;
}
public function export(KernelInterface $kernel): array
{
dd("why?!");
}
// ...
}
在 ressource/entity.yaml 中:
resources:
App\Entity\MyEntity:
shortName: ~
description: ~
attributes:
order:
referenceCafHuissier: asc
montantEcheance: asc
# security: 'is_granted("ROLE_USER")'
normalization_context:
groups: ['myentity:read']
denormalization_context:
groups: ['myentity:write']
properties:
...
collectionOperations:
...
exportcsv:
security: 'is_granted("ROLE_MYENTITY_ADMIN")'
# pagination_partial: true
method: 'GET'
path: '/myentity/export'
controller: 'App\Controller\MyEntityController::export'
openapi_context:
summary: Export CSV
parameters: []
responses:
'200':
description: Génération réussi de l'export CSV
content:
application/json:
schema:
type: object
properties:
type:
type: string
description: mime-type du fichier.
content:
type: string
description: Base64 du contenu du fichier.
uj5u.com熱心網友回復:
您正在談論的查詢很可能是由ReadListener.
要禁用它,設定read屬性false,如解釋在這里:
collectionOperations:
exportcsv:
method: get
path: /pensions/export
# ...
read: false
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406901.html
標籤:
上一篇:Symfony關系驗證
