繁体   English   中英

@paramconverter 注释 Symfony 找不到 App\\Entity\\Article 对象

[英]App\Entity\Article object not found by the @paramconverter annotation Symfony

我在修改评论后尝试重定向,但出现错误 App\\Entity\\Article object not found by the @paramconverter annotation 你知道这个问题吗?

/**
 * @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 )好像你是路过的id的的Comment ,而吴丹的id中的Article

没有任何明确的解释,Symfony 尝试在控制器参数( Article )中找到您的第一个 Entity 的id (您的 URL 参数的名称),但找不到它,因为您似乎正在尝试修改Comment

由于您希望通过 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
{
    // ...

然后在您的ArticleRepository创建一个名为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();
    // ...

更多关于参数转换的信息: https : //symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM