简体   繁体   English

无法使用 @ParamConverter 声明自动装配实体参数

[英]Cannot autowire entity argument with @ParamConverter declaration

I encountered an issue with @ParamConverter.我遇到了@ParamConverter 的问题。 Entities arguments are detected such as "service".检测到实体 arguments,例如“服务”。

I think that multiples route arguments and @ParamConverter are responsables of this issue.我认为多重路由 arguments 和 @ParamConverter 是这个问题的责任者。

I have tried to deactivate auto_convert option in sensio_framework_extra without success.我试图停用auto_convert中的sensio_framework_extra选项但没有成功。

Cannot autowire argument $category of "App\Controller\Front\QuestionController::showRandomAction()": it references class "App\Entity\Category" but no such service exists.

sensio_framework_extra.yaml sensio_framework_extra.yaml

sensio_framework_extra:
    router:
        annotations: false
    request:
        converters: true
        auto_convert: false

Controller Controller

/**
 * @Route("/{id}/{slug}/", name="app_front_question_show", methods={"GET", "POST"})
 * @ParamConverter(name="category", class="App\Entity\Category", options={"mapping": {"slug": "slug"}})
 * @ParamConverter(name="question", class="App\Entity\Question", options={"mapping": {"id": "id"}})
*/
public function showAction(
   Request $request,
   Category $category,
   Question $question,
   AnswerRepository $answerRepository,
   ReportQuestionManager $reportQuestionManager
) {
    // ...
}

Stack:堆:

* Symfony 4.4

It's because You're using param converters and you deactivate them with auto_convert: false .这是因为您正在使用参数转换器并使用auto_convert: false停用它们。 You can see documentation “You can disable the auto-conversion of type-hinted method arguments feature by setting the auto_convert flag to false”您可以查看文档“您可以通过将 auto_convert 标志设置为 false 来禁用类型提示方法 arguments 功能的自动转换”

Thank you for the response but Baptiste is right.感谢您的回复,但巴蒂斯特是对的。

I managed to do what I wanted by correcting the ParamConverter annotations.我设法通过更正 ParamConverter 注释来做我想做的事。

/**
 * @Route("/{id}/{slug}/", name="app_front_question_show", methods={"GET", "POST"})
 * @ParamConverter("category", class="App\Entity\Category", options={"mapping": {"slug": "slug"}})
 * @ParamConverter("question", class="App\Entity\Question", options={"mapping": {"id": "id"}})
*/
public function showAction(
   Request $request,
   Category $category,
   Question $question,
   AnswerRepository $answerRepository,
   ReportQuestionManager $reportQuestionManager
) {
    // ...
}

I should not put the "name=''"我不应该把“name=''”

The auto_convert: false is good and do what I want auto_convert: false很好,做我想做的事

I close the post.我关闭帖子。 Thank you谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法自动装配参数 $request - Cannot autowire argument $request Symfony:无法自动装配“Controller:method()”的参数 $injectedVar:它引用 class“App\Entity\Entity”,但不存在此类服务 - Symfony: Cannot autowire argument $injectedVar of "Controller:method()": it references class "App\Entity\Entity" but no such service exists ParamConverter:声明异常symfony2 - ParamConverter: Declaration exception symfony2 Symfony4。 ParamConverter注释与通过autowire注入服务冲突 - Symfony4. ParamConverter annotation conflicts injecting a service by autowire 无法自动装配“App\Controller\BlogController::postById()”的参数 $post - Cannot autowire argument $post of "App\Controller\BlogController::postById()" 无法自动装配服务“App\\Service\\MatchCarAdService”:方法的参数“$templating” - Cannot autowire service "App\Service\MatchCarAdService": argument "$templating" of method 无法自动装配服务:参数引用 class 但不存在此类服务 - Cannot autowire service: Argument references class but no such service exists Symfony 5 无法为动态数据库连接自动装配参数 $Driver - Symfony 5 Cannot autowire argument $Driver for a dynamic database connection “@ParamConverter 注释未找到 App\Entity\User object” - "App\Entity\User object not found by the @ParamConverter annotation" Symfony @ParamConverter 通过用户关系获取实体 - Symfony @ParamConverter get entity through user relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM