php 无法猜测如何从请求信息中获取 Doctrine 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9726452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to guess how to get a Doctrine instance from the request information
提问by copndz
I've got this "500 Internal Server Error - LogicException: Unable to guess how to get a Doctrine instance from the request information".
我收到了这个“500 内部服务器错误 - LogicException:无法猜测如何从请求信息中获取 Doctrine 实例”。
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?
它不起作用,可能是因为 Doctrine 2 无法“猜测”......那么我如何让 Doctrine 2 猜测呢?
采纳答案by copndz
/**
* @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.
我在 paramconverter 的(差的?)文档中没有找到任何帮助,因为它没有描述它是如何工作的,它是如何用多个参数和东西进行猜测的。另外,我不确定是否需要它,因为我刚刚编写的内容可以正常工作。
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}.
我的疑虑是不使用我的属性名称,因此教义无法猜对。我将 {player_name} 改为 {name},将 {gather_id} 改为 {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 :
然后我在他们的实体中将我的 id 名称从“id”更改为“id_gather”和“id_player”,所以我现在可以这样做:
/**
* @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)
回答by Azr
The Doctrine doesn't know how to use request parameters in order to query entities specified in the function's signature.
Doctrine 不知道如何使用请求参数来查询函数签名中指定的实体。
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)
{
// ...
}
回答by Lhassan Baazzi
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)
回答by ftassi
@1ed is right, you should define a @paramConverter in order to get a Player instance or a Gather instance.
@1ed 是对的,你应该定义一个 @paramConverter 以获得一个 Player 实例或一个 Gather 实例。
回答by Mohammed Réda OUASSINI
The parameters on the signature of the @Route annotation must match the entities fields, so that Doctrine makes automatically the convertion.
@Route 注解签名上的参数必须匹配实体字段,以便 Doctrine 自动进行转换。
Otherwise you need to do the convertion manually by using the annotation @ParamConverter as it's mentionned on the other responses.
否则,您需要使用注释 @ParamConverter 手动进行转换,因为它在其他响应中提到过。