【问题标题】:Unable to guess how to get a Doctrine instance from the request information for parameter "offer"无法猜测如何从参数“offer”的请求信息中获取 Doctrine 实例
【发布时间】:2021-06-27 23:09:19
【问题描述】:

我在致电我的实体 Offer 时遇到问题 我想调用方法 getApplication() 但我收到了这个错误 无法猜测如何从参数“offer”的请求信息中获取 Doctrine 实例。 有人知道如何调用实体吗?谢谢

<?php

namespace App\Controller;
use App\Entity\Application;
use App\Entity\Offer;
use App\Repository\ApplicationRepository;
use App\Repository\BrandRepository;
use App\Repository\OfferRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
 * Class OfferController
 * @package App\Controller
 *
 * @Route("/offer", name="offer_")
 */

class OfferController extends AbstractController
{
    /**
     * @Route("/", name="index")
     */

    public function index(ApplicationRepository $applicationRepository, OfferRepository $offerRepository, Offer $offer)
    {
        $user = $this->getUser();    
        $offers = $offerRepository->findBy([], ['dateCreation' => 'DESC']);
        $application = $applicationRepository->findOneBy(['id' => $offer->getApplication()]);

        return $this->render('offer/index.html.twig', [
            'offers' =>  $offers,
            'application' => $application,
            'offer' => $offer
        ]);
    }
}

【问题讨论】:

  • 您的路线/offer/ 不包含Doctrine can use to infer 您要使用的实体的slug。也许重新配置你的路线可以解决这个问题?

标签: php symfony


【解决方案1】:

如果您的实体有一个名为 id 的属性来唯一标识优惠,您可以利用参数转换器并将 {id} 参数添加到您的 URL。更多信息here.

<?php

namespace App\Controller;
use App\Entity\Application;
use App\Entity\Offer;
use App\Repository\ApplicationRepository;
use App\Repository\BrandRepository;
use App\Repository\OfferRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
 * Class OfferController
 * @package App\Controller
 *
 * @Route("/offer", name="offer_")
 */

class OfferController extends AbstractController
{
    /**
     * @Route("/{id}", name="index")
     */
    public function index(ApplicationRepository $applicationRepository, OfferRepository $offerRepository, Offer $offer)
    {
        $user = $this->getUser();
        $offers = $offerRepository->findBy([], ['dateCreation' => 'DESC']);
        $application = $applicationRepository->findOneBy(['id' => $offer->getApplication()]);

        return $this->render('offer/index.html.twig', [
            'offers' =>  $offers,
            'application' => $application,
            'offer' => $offer
        ]);
    }
}

【讨论】:

    猜你喜欢
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 2021-01-20
    • 2015-12-14
    • 1970-01-01
    • 2016-05-03
    • 2019-11-25
    相关资源
    最近更新 更多