duandai7601 2019-02-28 21:34
浏览 10

如何用Doctrine解决实例问题?

I'm new learner of Symfony 4 and I'm looking for help. I've an Entity named "Player" and I want to generate a random confirmation number.

For now, I'm using a variable $confirmNbr and I save the $confirm in my database with $participant->setConfirmationNumber($confirmNbr);.

What I want it's create a function generateRandomNumber() in my Entity Player.php like this :

public function generateConfirmationNumber() : self 
{

    $this->confirmationNumber = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',6)),0,5);

    return $this;
}

This is my Controller file

   /**
     * @Route("/", name="homepage")
     */
    public function new(Player $player, EntityManagerInterface $em, Request $request)
    {
        $participant = $this->playerrepo->findAll();

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

        $randomNbr = $player->generateConfirmationNumber();



        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
            $participant = new Player;
            $participant->setName($data['name']);
            $participant->setFirstname($data['firstname']);
            $participant->setEmail($data['email']);
            $participant->setConfirmationNumber($confirmNbr);
            $participant->setRegisterAt(new \DateTime);

            $em->persist($player);
            $em->flush();

            $this->addFlash('success', 'Player added!');

            return $this->redirectToRoute('homepage');
        }

        return $this->render('app/subscribe.html.twig', [
            'playerForm' => $form->createView(),
            'player'=>$player,
        ]);
    }

And this is my error message :

Unable to guess how to get a Doctrine instance from the request information for parameter "player".

Can you help me please ?

  • 写回答

2条回答 默认 最新

  • dongshao4207 2019-02-28 21:47
    关注

    Your method is expecting an instance of the Player object - where should it come from? Doctrine is trying to guess it and get it from the URL, but it cannot. Your method is for creating new players - why do you need an instance of a player? Just remove that parameter from the method signature, i.e. change it to:

    public function new(EntityManagerInterface $em, Request $request)

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度