首页 > 解决方案 > __construct 函数未在容器 symfony 3.4 中正确构造

问题描述

大家好,我有一个报告包和一个服务 AdminUsersStatsListBlockService 需要报告包中的 UserRepository.php 来访问函数,我试图将报告包中的 BookingBundle.php 添加到构造函数中,但我在没有它的情况下继续构建这是我的代码和我的错误:

HERE AdminUsersStatsListBlockService.php(所以我尝试在此处添加 BookingRepository):

<?php

/*
 * This file is part of the Cocorico package.
 *
 * (c) Cocolabs SAS <contact@cocolabs.io>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Cocorico\ReportBundle\Block\Service;

use Cocorico\BookingBundle\Entity\Booking;
use Cocorico\ReportBundle\Repository\UserRepository;
use Cocorico\ReportBundle\Repository\BookingRepository; /*(added)*/
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\CoreBundle\Validator\ErrorElement;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AdminUsersStatsListBlockService extends AbstractBlockService
{
    protected $userRepository;
    protected $bookingRepository;/*(added)*/
    protected $adminPool;

    /**
     * @param string          $name
     * @param EngineInterface $templating
     * @param UserRepository  $userRepository
     * @param Pool            $adminPool
     * @param BookingRepository  $bookingRepository/*(added)*/
     */
    public function __construct(
        $name,
        EngineInterface $templating,
        UserRepository $userRepository,
        Pool $adminPool = null,
        BookingRepository $bookingRepository/*(added)*/
    ) {
        parent::__construct($name, $templating);

        $this->userRepository = $userRepository;
        $this->bookingRepository = $bookingRepository;/*(added)*/
        $this->adminPool = $adminPool;
    }

    /**
     * {@inheritdoc}
     */
    public function execute(BlockContextInterface $blockContext, Response $response = null)
{
        $stat = $blockContext->getSetting('stat');
        switch ($stat) {
            case 'offerers-expiring':
                $results = $this->userRepository->getTopOfferersWithBookingsStatusCount(
                    Booking::STATUS_EXPIRED,
                    null,
                    null,
                    $blockContext->getSetting('limit')
                );
                break;
            case 'offerers-refusing':
                $results = $this->userRepository->getTopOfferersWithBookingsStatusCount(
                    Booking::STATUS_REFUSED,
                    null,
                    null,
                    $blockContext->getSetting('limit')
                );
                break;
            case 'offerers-accepting':
                $results = $this->userRepository->getTopOfferersWithBookingsStatusCount(
                    Booking::STATUS_PAYED,
                    null,
                    null,
                    $blockContext->getSetting('limit')
                );
                break;
            case 'bookings-expired-list':
                $results = $this->bookingRepository->getBookingsExpired(); /*there i want to use it*/
                break;
            default:
                $results = array();
        }

        return $this->renderResponse(
            $blockContext->getTemplate(),
            array(
                'block' => $blockContext->getBlock(),
                'settings' => $blockContext->getSettings(),
                'results' => $results,
                'admin_pool' => $this->adminPool,
            ),
            $response
        );
    }
}

这是我得到的错误:

request.CRITICAL:未捕获的 PHP 异常 Symfony\Component\Debug\Exception\FatalThrowableError:“类型错误:函数 Cocorico\ReportBundle\Block\Service\AdminUsersStatsListBlockService::__construct() 的参数太少,在 /var/www/Symfony 中传递了 4 个/var/cache/prod/Container7aqlalh/getCocoricoReport_Admin_Block_Users_StatsListService.php 在第 13 行,正好 5 预期”在 /var/www/Symfony/vendor/cocorico/report-bundle/Block/Service/AdminUsersStatsListBlockService.php 第 40 行 {“异常”: "[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Type error: Too little arguments to function Cocoric\ReportBundle\Block\Service\AdminUsersStatsListBlockService::__construct(), 4 传入 /var/www /Symfony/var/cache/prod/Container7aqlalh/getCocoricoReport_Admin_Block_Users_StatsListService。php 在第 13 行,而在 /var/www/Symfony/vendor/cocorico/report-bundle/Block/Service/AdminUsersStatsListBlockService.php:40)"} []

并且容器仍未使用 BookingRepository 构建:

<?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;

// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the public 'cocorico_report.admin.block.users.stats_list' shared service.

include_once $this->targetDirs[3].'/vendor/sonata-project/block-bundle/src/Block/BlockServiceInterface.php';
include_once $this->targetDirs[3].'/vendor/sonata-project/block-bundle/src/Block/Service/BlockServiceInterface.php';
include_once $this->targetDirs[3].'/vendor/sonata-project/block-bundle/src/Block/Service/AbstractBlockService.php';
include_once $this->targetDirs[3].'/vendor/cocorico/report-bundle/Block/Service/AdminUsersStatsListBlockService.php';

return $this->services['cocorico_report.admin.block.users.stats_list'] = new \Cocorico\ReportBundle\Block\Service\AdminUsersStatsListBlockService('cocorico_report.admin.block.users.stats_list', ${($_ = isset($this->services['templating']) ? $this->services['templating'] : $this->load('getTemplatingService.php')) && false ?: '_'}, ${($_ = isset($this->services['cocorico_report.user.repository']) ? $this->services['cocorico_report.user.repository'] : $this->load('getCocoricoReport_User_RepositoryService.php')) && false ?: '_'}, ${($_ = isset($this->services['sonata.admin.pool']) ? $this->services['sonata.admin.pool'] : $this->getSonata_Admin_PoolService()) && false ?: '_'});

编辑:找到这个,这是装载机吗?:

services:
    cocorico_report.admin.block.stats:
        class: Cocorico\ReportBundle\Block\Service\AdminStatsBlockService
        arguments:
            - "cocorico_report.admin.block.stats"
            - "@templating"
            - "@cocorico_report.report.manager"
        tags:
            - { name: sonata.block }

    cocorico_report.admin.block.users.stats_list:
        class: Cocorico\ReportBundle\Block\Service\AdminUsersStatsListBlockService
        arguments:
            - "cocorico_report.admin.block.users.stats_list"
            - "@templating"
            - "@cocorico_report.user.repository"
            - "@sonata.admin.pool"
        tags:
            - { name: sonata.block }

提前感谢您的帮助!:)

标签: phpsymfonycontainers

解决方案


因此,您需要做的就是将预订存储库添加到服务定义中。

# ReportBundle/Resources/services.yml
services:
    cocorico_report.admin.block.users.stats_list:
        class: Cocorico\ReportBundle\Block\Service\AdminUsersStatsListBlockService
        arguments:
            - "cocorico_report.admin.block.users.stats_list"
            - "@templating"
            - "@cocorico_report.user.repository"
            - "@sonata.admin.pool"
            - "@cocorico_report.booking.repository" # ADD THIS #
        tags:
            - { name: sonata.block }

这里的假设是您还已经定义了 cocorico_report.booking.repository 服务。如果存储库是您添加的内容,那么您将必须找到用户存储库服务定义并基本上克隆它。

有更多关于如何显式配置服务的信息。尽量避免将其与自动装配方法混淆。


推荐阅读