Symfony 6 App \ Entity \问题对象未找到@ParamConverter注释

发布于 2025-01-28 05:29:53 字数 1503 浏览 1 评论 0原文

我尝试提出问题的答案。

这是我的答案:

#[Route('/{slug}/{name}/answer/{question}', name: 'answer_question')]
public function answer(Question $q, QuestionRepository $questionRepository, string $question, Request $request, EntityManagerInterface $entityManager, string $slug, string $name, AnswerRepository $answerRepository): Response
{
    $questions = $questionRepository->findOneBySlug($question);

    $answers = $answerRepository->findAnswers($question);

    $form = $this->createForm(AnswerType::class);

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $data = $form->getData();

        $answer = new Answer();
        $answer->setContent($data->getContent());
        $answer->setAnsweredAt(new \DateTime);
        $answer->setQuestion($q);

        $entityManager->persist($answer);
        $entityManager->flush();

        return $this->redirectToRoute('show_question', [
            'slug' => $slug,
            'name' => $name,
            'question' => $question,
        ]);
    }

    return $this->render('answer/index.html.twig', [
        'questions' => $questions,
        'answers' => $answers,
        'answer' =>  $form->createView(),
    ]);
}

我对

app \ entity \ Question对象

问题

的问题有 例如,当我放置地址“/audi-a3/8v/anders/ateys”时,这没关系,但是当我放置“/audi-a4/8v/wonse/ateys”时(没有Audi-a4与8V模型,但这地址为“ 8V”模型提供了正确的页面。 有人可以向我解释如何做正确吗?

I try to make answers for question.

Here is my AnswerController:

#[Route('/{slug}/{name}/answer/{question}', name: 'answer_question')]
public function answer(Question $q, QuestionRepository $questionRepository, string $question, Request $request, EntityManagerInterface $entityManager, string $slug, string $name, AnswerRepository $answerRepository): Response
{
    $questions = $questionRepository->findOneBySlug($question);

    $answers = $answerRepository->findAnswers($question);

    $form = $this->createForm(AnswerType::class);

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $data = $form->getData();

        $answer = new Answer();
        $answer->setContent($data->getContent());
        $answer->setAnsweredAt(new \DateTime);
        $answer->setQuestion($q);

        $entityManager->persist($answer);
        $entityManager->flush();

        return $this->redirectToRoute('show_question', [
            'slug' => $slug,
            'name' => $name,
            'question' => $question,
        ]);
    }

    return $this->render('answer/index.html.twig', [
        'questions' => $questions,
        'answers' => $answers,
        'answer' =>  $form->createView(),
    ]);
}

I have problem with

App\Entity\Question object not found by the @ParamConverter annotation

EDIT:

Still I have a little problem with wildcards,
for example when i put address "/audi-a3/8v/answer/question" it's ok, but when i put "/audi-a4/8v/answer/question" ( there isn't audi-a4 with model 8v but this address gives me correct page for "8V" model. It doesn't match ).
Can someone explain me how to do it right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2025-02-04 05:29:53

我对AnswerController进行了一些更改,现在正在工作。

手动设置paramconverter。

#[Route('/{car}/{name}/answer/{slug}', name: 'answer_question')]
#[ParamConverter('question', class: Question::class, options: ['mapping' => ['slug' => 'slug']])]
public function answer(Question $question, QuestionRepository $questionRepository, Request $request, EntityManagerInterface $entityManager, string $slug, string $name, string $car, AnswerRepository $answerRepository): Response
{
    $questions = $questionRepository->findOneBySlug($slug);

    $answers = $answerRepository->findAnswers($slug);

    $form = $this->createForm(AnswerType::class);

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $data = $form->getData();

        $answer = new Answer();
        $answer->setContent($data->getContent());
        $answer->setAnsweredAt(new \DateTime);
        $answer->setQuestion($question);

        $entityManager->persist($answer);
        $entityManager->flush();

        return $this->redirectToRoute('show_question', [
            'slug' => $slug,
            'name' => $name,
            'car' => $car,
        ]);
    }

    return $this->render('answer/index.html.twig', [
        'questions' => $questions,
        'answers' => $answers,
        'answer' =>  $form->createView(),
    ]);
}

I did some changes in AnswerController and now it's working.

Set ParamConverter manually.

#[Route('/{car}/{name}/answer/{slug}', name: 'answer_question')]
#[ParamConverter('question', class: Question::class, options: ['mapping' => ['slug' => 'slug']])]
public function answer(Question $question, QuestionRepository $questionRepository, Request $request, EntityManagerInterface $entityManager, string $slug, string $name, string $car, AnswerRepository $answerRepository): Response
{
    $questions = $questionRepository->findOneBySlug($slug);

    $answers = $answerRepository->findAnswers($slug);

    $form = $this->createForm(AnswerType::class);

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $data = $form->getData();

        $answer = new Answer();
        $answer->setContent($data->getContent());
        $answer->setAnsweredAt(new \DateTime);
        $answer->setQuestion($question);

        $entityManager->persist($answer);
        $entityManager->flush();

        return $this->redirectToRoute('show_question', [
            'slug' => $slug,
            'name' => $name,
            'car' => $car,
        ]);
    }

    return $this->render('answer/index.html.twig', [
        'questions' => $questions,
        'answers' => $answers,
        'answer' =>  $form->createView(),
    ]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文