Symfony ParamConverter 到 MapEntity

问题描述 投票:0回答:1

我正在尝试将我的symfony应用程序从版本5.4升级到6.4,因为Sensio Bundle已被放弃,我需要使用属性而不是注释,我如何将paramcoverter注释转换为属性,下面是代码

class QuoteDataController extends AbstractController
{
    /**
     * @Route("/data/quote/list", name="data_quote_list")
     * @ParamConverter("query", class="App:ListQuery")
     */
    public function dataQuoteList(ListQuery $query, SessionInterface $session): JsonResponse
    {
        try {
            /** @var Contact $user */
            $user = $this->getUser();
            $session->set('QuoteQuery', $query);
            $repo = $this->getDoctrine()
                ->getRepository(Quote::class);

            $payloads = $repo->loadRecords($query, $user);

            return new JsonResponse([
                'total' => empty($payloads) ? 0 : $payloads[0]->getMeta()['found'],
                'rows' => $repo->tableise($payloads),
            ]);
        } catch (Throwable $e) {
            // echo $e;

            return new JsonResponse([
                'total' => 0,
                'rows' => [],
            ]);
        }
    }
}

我尝试了下面的代码,但它没有按预期工作

 #[Route('/data/quote/list', name: 'data_quote_list')]
 #[Entity('query', class: ListQuery::class)]```

it seems like old paramconverter has a custom custom paramconverter class, old developer worked on that , i am new to this project.

<?php

声明(strict_types=1);

命名空间 App\ParamConverter;

使用App\Entity\ListQuery; 使用 Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; 使用 Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; 使用 Symfony\Component\HttpFoundation\Request; 使用 Throwable;

类 ListQueryConverter 实现 ParamConverterInterface { 公共函数 apply(Request $request, ParamConverter $configuration): bool { $查询 = 新的列表查询( $request->query->get('搜索'), $request->query->getInt('offset') ?? 0, null === $request->query->get('limit') ? 5 : $request->query->getInt('limit'), $request->query->get('sort') ?? '', $this->defineSortOrder($request->query->get('order')), $this->setFilter($request->query->get('filter')) );

    $request->attributes->set($configuration->getName(), $query);

    return true;
}

public function supports(ParamConverter $configuration): bool
{
    return 'App:ListQuery' === $configuration->getClass();
}

private function determineSortOrder(?string $order): string
{
    if (ListQuery::SORT_ASCENDING === strtoupper($order)) {
        return ListQuery::SORT_ASCENDING;
    }

    return ListQuery::SORT_DESCENDING;
}

private function setFilter(?string $json): array
{
    if (null === $json) {
        return [];
    }

    try {
        return json_decode($json, true, 512, JSON_THROW_ON_ERROR);
    } catch (Throwable $exception) {
        return [];
    }
}

}

php symfony doctrine-orm doctrine
1个回答
0
投票

如果用于获取实体的数据位于请求查询内,则可以使用表达式来执行此操作。

我不确定您在查询中设置的数据,因此您可能需要调整此代码:

#[Route('/data/quote/list', name: 'data_quote_list')]
public function dataQuoteList(
    #[MapEntity(expr: 'repository.findOneBy({"yourFieldToSearch": request.query.get("query")})')]
    ListQuery $listQuery,
    SessionInterface $session
): Response {
}

您可以在这里找到更多信息(symfony 文档)。

© www.soinside.com 2019 - 2024. All rights reserved.