【问题标题】:Symfony3.4: Unable to guess how to get a Doctrine instance from the request information for parameterSymfony3.4:无法从参数的请求信息中猜测如何获取Doctrine实例
【发布时间】:2021-02-12 09:44:03
【问题描述】:

在 Symfony3.4 中,支持自动布线时出现以下错误。
当我单击链接时,出现以下错误。
其他转换工作正常。你有什么想法吗?

https://symfony.com/doc/3.4/service_container/3.3-di-changes.html

错误

Unable to guess how to get a Doctrine instance from 
the request information for parameter "keepRequest".
//stack trace
LogicException:
Unable to guess how to get a Doctrine instance from the request information for parameter "keepRequest".

  at vendor/sensio/framework-extra-bundle/Request/ParamConverter/DoctrineParamConverter.php:97
  at Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter->apply(object(Request), object(ParamConverter))
     (vendor/sensio/framework-extra-bundle/Request/ParamConverter/ParamConverterManager.php:92)
  at Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager->applyConverter(object(Request), object(ParamConverter))
     (vendor/sensio/framework-extra-bundle/Request/ParamConverter/ParamConverterManager.php:48)
  at Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager->apply(object(Request), array('amcService' => object(ParamConverter), 'keepRequestService' => object(ParamConverter), 'ecItemService' => object(ParamConverter), 'keepRequest' => object(ParamConverter)))
     (vendor/sensio/framework-extra-bundle/EventListener/ParamConverterListener.php:78)
  at Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener->onKernelController(object(FilterControllerEvent), 'kernel.controller', object(TraceableEventDispatcher))
     (vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php:115)
  at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(FilterControllerEvent), 'kernel.controller', object(TraceableEventDispatcher))
     (vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:214)
  at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.controller', object(FilterControllerEvent))
     (vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:44)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.controller', object(FilterControllerEvent))
     (vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php:143)
  at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.controller', object(FilterControllerEvent))
     (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:139)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:68)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:200)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (web/app_dev.php:37)

KeepRequestController.php

/**
 *
 * @Route("/hq/keepRequest")
 */
class KeepRequestController extends BaseKeepRequestController
{
    protected $indexRoute = "app_ahi_sp_admin_hq_keeprequest_index";
    /**
     *
     * @Method("GET")
     * @Route("/")
     * @Template("AppAhiSpAdminBundle:Hq/KeepRequest:index.html.twig")
    public function indexAction(Request $request, AmcService $amcService, KeepRequestService $keepRequestService, EcItemService $ecItemService, KeepRequest $keepRequest)
    {
        return parent::indexAction($request, $amcService, $keepRequestService, $ecItemService, $keepRequest);
    }

html.twig

  <a href="{{ path('app_ahi_sp_admin_hq_keeprequest_index') }}">
     <span>Over-the-counter fitting reservation</span><i class="icon-chevron-right pull-right"></i>
  </a>

BaseKeepRequestController.php

class BaseKeepRequestController extends BaseController
{
    protected $indexRoute;

    /**
     * List screen display action
     * @param Request $request
     * @param AmcService $amcService
     * @param KeepRequestService $keepRequestService
     * @param EcItemService $ecItemService
     * @return array
     */
    protected function indexAction(Request $request, AmcService $amcService, KeepRequestService $keepRequestService, EcItemService $ecItemService)
    {
        // Create a search form
        $searchForm = $this->createForm(KeepRequestSearchType::class, null, array(
            "action" => $this->generateUrl($this->indexRoute),
            "login_staff" => $this->getStaff(),
        ));

        // Get search criteria
        $params = $this->getSearchParameter($searchForm, $request);
        if (!$this->getStaff()->isHq()) {
            $params['shop'] = $this->getStaff()->getShop();
        }

        $amcParams = array();
        foreach ($params as $key => $value) {
            if (strpos($key, 'amcMember') === 0 && $value) {
                $amcParams[str_replace('amcMember', 'member', $key)] = $value;
            }
        }
        if ($amcParams) {
            $params['amcMember'] = $amcService->getMemberIdArray($amcParams);
        }

        // Create page nation
        $count = $keepRequestService->countKeepRequestBySearchParams($params);
        $page = $request->query->getInt('page', 1);
        $num = $request->query->getInt('num', 10);
        $pagination = new Pagination($count, $page, $num, 10);

        // Obtaining a reserve request
        $keepRequests = $keepRequestService->getKeepRequestBySearchParams(
            $keepRequest, $amcService, $ecItemService, $params,
            $pagination->getItemsPerPage(),
            $pagination->getSelectedPageOffset()
        );

        //Show screen
        return array(
            "searchForm" => $searchForm->createView(),
            "keepRequests" => $keepRequests,
            "pagination" => $pagination,
        );
    }

KeepRequestService.php

    /**
     * Registration
     *
     * @param KeepRequest $keepRequest Layaway request
     * @param \App\Ahi\Sp\CommonBundle\Model\Service\MailService $mailService
     * @param Mail $mail
     * @param LoggerInterface $logger
     * @param \App\Ahi\Sp\CommonBundle\Model\Service\AmcService $amcService
     * @param \App\Ahi\Sp\CommonBundle\Model\Service\EcItemService $ecItemService
     * @param Environment $twig
     */
    public function save(KeepRequest $keepRequest, MailService $mailService, Mail $mail, LoggerInterface $logger, AmcService $amcService, EcItemService $ecItemService, Environment $twig, $comment = null, $resend = false, $remind = false)
    {
        // Acquire product information and AMC member member information
        $this->getExtraData($keepRequest, $amcService, $ecItemService);

        // Set a reserve period when inventory is secured
        if ($keepRequest->getStatus() == 'keeping' && !$keepRequest->getKeepStartDate()) {
            list($keepStartDate, $keepEndDate) = $this->getKeepStartEndDate();
            $keepRequest->setKeepStartDate($keepStartDate);
            $keepRequest->setKeepEndDate($keepEndDate);
        }

        // Get the status before change from the last history
        $oldStatus = null;
        if (count($keepRequest->getHistories()) > 0) {
            $oldStatus = $keepRequest->getHistories()->last()->getStatus();
        }

        // Add history
        $keepRequest->addHistory($this->createHistory($keepRequest, $comment));

        // Reserve request registration/change
        $this->entityManager->transactional(function ($em) use ($keepRequest) {
            if ($keepRequest->getId()) {
                $this->entityManager->merge($keepRequest);
            } else {
                // Generate a reception number from id at the time of new registration
                $this->entityManager->persist($keepRequest);
                $this->entityManager->flush();
                $requestNo = sprintf("%4s%08d", date_format($keepRequest->getRequestedAt(), 'ym'), $keepRequest->getId());
                $keepRequest->setRequestNo($requestNo);
            }
        });
        ...

routing.yml

app_ahi_sp_admin:
  resource: '@AppAhiSpAdminBundle/Controller/'
  type: annotation
  prefix: /admin/
  schemes: [ http ]

KeepRequest.php(实体)

/**
 * @ORM\Entity(repositoryClass="App\Ahi\Sp\CommonBundle\Model\Repository\KeepRequestRepository")
 * @ORM\Table(name="keep_request")
 * @ORM\HasLifecycleCallbacks
 */
class KeepRequest
{
 //
}

KeepRequestRepository.php

    /**
     * Get articles that match the inspection conditions
     * @param array $params
     * @param null $limit
     * @param null $offset
     * @return array
     */
    public function getKeepRequestBySearchParams(array $params, $limit = null, $offset = null)
    {
        if (isset($params['amcMember']) && empty($params['amcMember'])) {
            return array();
        }

        $queryBuilder = $this->createQueryBuilder("h")
            ->select("h")
            ->orderBy("h.requestedAt", "desc");

        if ($limit !== null) {
            $queryBuilder->setMaxResults($limit);
        }

        if ($offset !== null) {
            $queryBuilder->setFirstResult($offset);
        }

        $this->addSearchParams($queryBuilder, $params);

        return $queryBuilder->getQuery()->getResult();
    }

尝试过的代码 以下代码已添加到 Controller、BaseController 和 Service。

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

     * @ParamConverter("keepRequest", class="AppAhiSpCommonBundle:KeepRequest")
     */
    public function indexAction(Request $request, AmcService $amcService, KeepRequestService $keepRequestService, EcItemService $ecItemService, KeepRequest $keepRequest)
    {

【问题讨论】:

  • KeepRequest 中有什么?我认为它在ParamConverter 中。 KeepRequest 是服务吗?
  • @jona303 感谢您的评论。 KeepRequest 是一个实体。
  • 请分享更多细节 - 您尝试过什么来调试问题?如果你的控制器期望接收任何实体,如果你的路由不包含任何 ID,它应该来自哪里?
  • @NicoHaase 请确认已添加 Service 和父控制器方法。 ID 将由@Route("/hq/keepRequest ") 指定。
  • 请分享更多细节 - 我在哪里可以找到包含要使用的 ID 的路由定义?

标签: php symfony


【解决方案1】:

好的,这是ParamConverter 的问题。你有一个 Route 和 0 个参数 @Route("/")。这条路由被分配给一个方法,该方法在它的参数KeepRequest 中请求一个实体。 转换的参数是找到要提供给路由方法的实体的服务。它需要一个参数来准确知道要提供哪个实体。

@Route("/{id}") ParamConverter 知道它必须通过 KeepRequest 的 id 和路由参数中提供的值来搜索它。您可以使用所有 KeepRequest 属性来执行此操作,但我建议您使用唯一的一次。 https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html

【讨论】:

  • 感谢您的回答。 ` 中包含的所有代码都在句子的末尾,所以请考虑修复它。我觉得比较好理解。使用@ParamConverter 更好吗? symfony 5 及以上版本似乎支持它。
  • 我不知道如何更好地解释这一点。通过在您的方法中添加实体 arg,您实际上使用了 ParamConverter。这就像你要求 symfony 给你一个实体,而 symfony 回答说我不知道​​我必须给你一个。因此,在您的路由中添加一个参数,例如 @Route("/{id}") 或删除该方法的参数列表的实体。
  • 糟糕。由于浏览器的影响,它只是崩溃了。现在看起来很漂亮。由于相关部分是文章列表屏幕,因此不需要 id。感觉参数是@Route("/hq/keepRequest")传的。还是Entity变成服务有问题?
  • 删除您的 KeepRequest 参数。
  • 当我删除 KeepRequest 时,我收到了错误 Notice: Undefined variable: keepRequest。已添加Service和BaseKeepRequestController.php($keepRequest原来有),请检查一下。
猜你喜欢
  • 2019-08-05
  • 2021-06-27
  • 1970-01-01
  • 2012-04-01
  • 2021-01-20
  • 2015-12-14
  • 1970-01-01
  • 2016-05-03
  • 2019-11-25
相关资源
最近更新 更多