Unable to guess how to get a Doctrine instance fro

2019-04-03 11:34发布

I've got this "500 Internal Server Error - LogicException: Unable to guess how to get a Doctrine instance from the request information".

Here is my controller's action definition:

/**
 * @Route("/gatherplayer/{player_name}/{gather_id}")
 * @Template()
 */
public function createAction(Player $player, Gather $gather)
{
  // ...
}

And it doesn't work, probably because Doctrine 2 can not "guess"... So how do I make Doctrine 2 guess, and well?

5条回答
时光不老,我们不散
2楼-- · 2019-04-03 12:00

@1ed is right, you should define a @paramConverter in order to get a Player instance or a Gather instance.

查看更多
ら.Afraid
3楼-- · 2019-04-03 12:06

The parameters on the signature of the @Route annotation must match the entities fields, so that Doctrine makes automatically the convertion.

Otherwise you need to do the convertion manually by using the annotation @ParamConverter as it's mentionned on the other responses.

查看更多
我命由我不由天
4楼-- · 2019-04-03 12:07

try this:

/**
 * @Route("/gatherplayer/{player_name}/{gather_id}")
 * @ParamConverter("player", class="YourBundle:Player")
 * @ParamConverter("gather", class="YourBundle:Gather")
 * @Template()
 */
public function createAction(Player $player, Gather $gather)
查看更多
对你真心纯属浪费
5楼-- · 2019-04-03 12:19
/**
 * @Route("/gatherplayer/{name}/{id}")
 * @Template()
 */
public function createAction(Player $player, Gather $gather)

I didn't find any help in paramconverter's (poor?) documentation, since it doesn't describe how it works, how it guesses with more than one parameters and stuff. Plus I'm not sure it's needed since what I just wrote works properly.

My mystake was not to use the name of my attributs so doctrine couldn't guess right. I changed {player_name} to {name} and {gather_id} to {id}.

Then I changed the names of my id in their entities from "id" to "id_gather" and "id_player" so I'm now able to do that :

/**
 * @Route("/gatherplayer/{id_player}/{id_gather}")
 * @Template()
 */
public function createAction(Player $player, Gather $gather)

which is a lot more effective than

 * @Route("/gatherplayer/{id}/{id}")

Now I'm wondering how I can make this work

 /**
  * @Route("/gatherplayer/{player}/{gather}")
  * @Template()
  */
 public function deleteAction(Gather_Player $gather_player)
查看更多
冷血范
6楼-- · 2019-04-03 12:22

The Doctrine doesn't know how to use request parameters in order to query entities specified in the function's signature.

You will need to help it by specifying some mapping information:

/**
  * @Route("/gatherplayer/{player_name}/{gather_id}")
  *
  * @ParamConverter("player", options={"mapping": {"player_name" : "name"}})
  * @ParamConverter("gather", options={"mapping": {"gather_id"   : "id"}})
  *
  * @Template()
  */
public function createAction(Player $player, Gather $gather)
{
  // ...
}
查看更多
登录 后发表回答