我如何指定先前表格的对象?

发布于 2025-01-11 03:26:08 字数 1868 浏览 0 评论 0原文

在我的项目中,我想使用由先前表单创建的对象:

这是我的数据库的架构:

my database

My QuizController

public function creation(Request $request){
        $quiz = new Quiz();
        $user = $this->getUser();

        $formQuiz = $this->createForm(QuizType::class, $quiz);

        $formQuiz->handleRequest($request);
  

        if ($formQuiz->isSubmitted() && $formQuiz->isValid() ) {
            $quiz->setCreatedAt(new DateTimeImmutable());
            $quiz->setCreatedBy($user);
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($quiz);
            $entityManager->flush();

            return $this->redirectToRoute('creation_questions');
        }
        return $this->render('quiz/creation.html.twig', [
            'formQuiz' => $formQuiz->createView(),
        ]);
    }

和我的 QuestionController 必须与测验表单连接

public function creation_questions(Request $request){
        $quiz = ?
        $question = new Questions();

        $formQuestions = $this->createForm(QuestionType::class, $question);
        
        $formQuestions->handleRequest($request);

        if ($formQuestions->isSubmitted() && $formQuestions->isValid() ) {
            $question->setCreatedAt(new DateTimeImmutable());
            $question->setQuiz($quiz);
 
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($question);
  
            $entityManager->flush();

            return $this->redirectToRoute('home');
        }
        return $this->render('questions/questions.html.twig', [
            'formQuestion' => $formQuestions->createView()
        ]);
    }

我必须写什么来代替“?”?

In my project I want to use the object created by my precedent form:

Here is the schema of my database:

my database

My QuizController

public function creation(Request $request){
        $quiz = new Quiz();
        $user = $this->getUser();

        $formQuiz = $this->createForm(QuizType::class, $quiz);

        $formQuiz->handleRequest($request);
  

        if ($formQuiz->isSubmitted() && $formQuiz->isValid() ) {
            $quiz->setCreatedAt(new DateTimeImmutable());
            $quiz->setCreatedBy($user);
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($quiz);
            $entityManager->flush();

            return $this->redirectToRoute('creation_questions');
        }
        return $this->render('quiz/creation.html.twig', [
            'formQuiz' => $formQuiz->createView(),
        ]);
    }

And my QuestionController that must be connected with the quiz form

public function creation_questions(Request $request){
        $quiz = ?
        $question = new Questions();

        $formQuestions = $this->createForm(QuestionType::class, $question);
        
        $formQuestions->handleRequest($request);

        if ($formQuestions->isSubmitted() && $formQuestions->isValid() ) {
            $question->setCreatedAt(new DateTimeImmutable());
            $question->setQuiz($quiz);
 
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($question);
  
            $entityManager->flush();

            return $this->redirectToRoute('home');
        }
        return $this->render('questions/questions.html.twig', [
            'formQuestion' => $formQuestions->createView()
        ]);
    }

What do I have to write in place of the '?'?

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

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

发布评论

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

评论(1

断爱 2025-01-18 03:26:08

您不显示您的路由,但您可以使用 SensioFrameworkExtraBundle 中的 paramConverte“magic” 并执行类似的操作。

/**
 * @Route("/some-route/{id}", name="some_route_name")
 */
public function creation_questions(Request $request, Quiz $quiz)
{
    $question = new Questions();

    $formQuestions = $this->createForm(QuestionType::class, $question);

    $formQuestions->handleRequest($request);

    if ($formQuestions->isSubmitted() && $formQuestions->isValid()) {
        $question->setCreatedAt(new DateTimeImmutable());
        $question->setQuiz($quiz);

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($question);

        $entityManager->flush();

        return $this->redirectToRoute('home');
    }
    return $this->render('questions/questions.html.twig', [
        'formQuestion' => $formQuestions->createView()
    ]);
}

其中 /someRoute/{id}{id} 部分是测验 ID。 Symfony 应该自动获取与该 id 匹配的测验。或者您可以更明确地说明参数转换器应如何解释此类值。更多信息请点击这里 https://symfony.com/bundles/SensioFrameworkExtraBundle/current/annotations /converters.html

或者,您可以传递测验 ID 并手动获取测验(不太神奇,但完全合法)。

/**
 * @Route("/some-route/{id}", name="some_route_name")
 */
public function creation_questions(Request $request, int $id)
{
    $entityManager = $this->getDoctrine()->getManager();
    $quiz = $entityManager->getRepository(Quiz::class)->find($id);
    $question = new Questions();

    $formQuestions = $this->createForm(QuestionType::class, $question);

    $formQuestions->handleRequest($request);

    if ($formQuestions->isSubmitted() && $formQuestions->isValid()) {
        $question->setCreatedAt(new DateTimeImmutable());
        $question->setQuiz($quiz);

        $entityManager->persist($question);

        $entityManager->flush();

        return $this->redirectToRoute('home');
    }
    return $this->render('questions/questions.html.twig', [
        'formQuestion' => $formQuestions->createView()
    ]);
}

You don't show your routing but you could use paramConverte "magic" from SensioFrameworkExtraBundle and do something like this.

/**
 * @Route("/some-route/{id}", name="some_route_name")
 */
public function creation_questions(Request $request, Quiz $quiz)
{
    $question = new Questions();

    $formQuestions = $this->createForm(QuestionType::class, $question);

    $formQuestions->handleRequest($request);

    if ($formQuestions->isSubmitted() && $formQuestions->isValid()) {
        $question->setCreatedAt(new DateTimeImmutable());
        $question->setQuiz($quiz);

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($question);

        $entityManager->flush();

        return $this->redirectToRoute('home');
    }
    return $this->render('questions/questions.html.twig', [
        'formQuestion' => $formQuestions->createView()
    ]);
}

Where the {id} part of /someRoute/{id} is the Quiz Id. Symfony should automagically fetch the Quiz matching that id. Or you can be more explicit about how the param converter should interpret such a value. More info here https://symfony.com/bundles/SensioFrameworkExtraBundle/current/annotations/converters.html

Alternatively, you could pass the quiz id and fetch the quiz manually (less magic but totally legit).

/**
 * @Route("/some-route/{id}", name="some_route_name")
 */
public function creation_questions(Request $request, int $id)
{
    $entityManager = $this->getDoctrine()->getManager();
    $quiz = $entityManager->getRepository(Quiz::class)->find($id);
    $question = new Questions();

    $formQuestions = $this->createForm(QuestionType::class, $question);

    $formQuestions->handleRequest($request);

    if ($formQuestions->isSubmitted() && $formQuestions->isValid()) {
        $question->setCreatedAt(new DateTimeImmutable());
        $question->setQuiz($quiz);

        $entityManager->persist($question);

        $entityManager->flush();

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