Php @paramconverter注释Symfony找不到App\Entity\Article对象
我试图在修改注释后重定向,但@paramconverter注释未找到错误App\Entity\Article对象。您知道吗问题出在哪里Php @paramconverter注释Symfony找不到App\Entity\Article对象,php,symfony,Php,Symfony,我试图在修改注释后重定向,但@paramconverter注释未找到错误App\Entity\Article对象。您知道吗问题出在哪里 /** * @Route("/{id}/modifier", name="comment_edit", methods={"GET","POST"}, requirements={"id" = "\d+"}) * @param Requ
/**
* @Route("/{id}/modifier", name="comment_edit", methods={"GET","POST"}, requirements={"id" = "\d+"})
* @param Request $request
* @param Article $article
* @param Comment $comment
* @param LinkRepository $linkRepository
* @param MoreRepository $moreRepository
* @return Response
*/
public function editComment(Request $request,Article $article, Comment $comment, LinkRepository $linkRepository,MoreRepository $moreRepository): Response
{
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('article_details',['id' => $article->getId()]);
}
}
使用函数名(
editComment
)时,似乎您传递的是Comment
的id
,而不是文章的id
在没有任何明确解释的情况下,Symfony尝试在控制器参数(一篇文章
)中查找第一个实体的id
(URL参数的名称),但找不到它,因为您似乎在试图修改注释
由于您希望通过URL参数对多个对象进行水合处理,因此应明确说明如何查找每个对象:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
/**
* @Route("/{id_comment}/modifier", name="edit_comment")
* @Entity("comment", expr="repository.find(id_comment)")
* @Entity("article", expr="repository.findArticleByCommentID(id_comment)")
*/
public function editComment(Request $request, Article $article, Comment $comment, LinkRepository $linkRepository, MoreRepository $moreRepository): Response
{
// ...
然后在文章库中创建一个名为findArticleByCommentID()
的函数
如果只希望通过注释找到$comment
,还可以执行以下操作:
/**
* @Route("/{id_comment}/modifier", name="edit_comment")
* @Entity("comment", expr="repository.find(id_comment)")
*/
public function editComment(Request $request, Comment $comment, LinkRepository $linkRepository, MoreRepository $moreRepository): Response
{
// Notice there isn't any Article in the controller arguments
$article = $comment->getArticle();
// ...
有关参数转换的更多信息,请参见:对象文章的名称空间是什么?是否使用导入
引用对象原点?看起来您没有导入此对象,因此它使用默认名称空间。尝试对设备使用import
,或在注释@param full\class\path\Device
中使用完整的类路径。您是否有use App\Entity\Article代码>行?(或者无论文章实体的名称空间是什么)是的,我使用App\Entity\Article