0

Hello all i got a a report-bundle and a service AdminUsersStatsListBlockService that need a UserRepository.php within the report bundle to access function, i tried to add the BookingBundle.php within the report bundle to the construct function but i keep constructing without it here's my code and my errors:

HERE AdminUsersStatsListBlockService.php (so i tried to add the BookingRepository here):

<?php

/*
 * This file is part of the Cocorico package.
 *
 * (c) Cocolabs SAS <[email protected]>
 *
 * 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
        );
    }
}

Here's the error i get:

request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Type error: Too few arguments to function Cocorico\ReportBundle\Block\Service\AdminUsersStatsListBlockService::__construct(), 4 passed in /var/www/Symfony/var/cache/prod/Container7aqlalh/getCocoricoReport_Admin_Block_Users_StatsListService.php on line 13 and exactly 5 expected" at /var/www/Symfony/vendor/cocorico/report-bundle/Block/Service/AdminUsersStatsListBlockService.php line 40 {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Type error: Too few arguments to function Cocorico\ReportBundle\Block\Service\AdminUsersStatsListBlockService::__construct(), 4 passed in /var/www/Symfony/var/cache/prod/Container7aqlalh/getCocoricoReport_Admin_Block_Users_StatsListService.php on line 13 and exactly 5 expected at /var/www/Symfony/vendor/cocorico/report-bundle/Block/Service/AdminUsersStatsListBlockService.php:40)"} []

and the container still not constructing with the 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 ?: '_'});

Edit: found this, is this the loader ? :

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 }

Thanks in advance for help!:)

10
  • 1
    Pool $adminPool is optional, but BookingRepository $bookingRepository is not. Try switching them around. Commented Oct 17, 2019 at 16:14
  • Does your cache need to be cleared in the container? (/var/www/Symfony/var/cache/prod) Commented Oct 17, 2019 at 16:22
  • I'll try that thanks @aynber, and Alex Barker i clean the cache everytime yes thanks Commented Oct 17, 2019 at 17:19
  • @aynber Where can i see what function is receiving these arguments ? Commented Oct 17, 2019 at 17:29
  • Receiving is AdminUsersStatsListBlockService::__construct(). Sending, try searching for grep -R AdminUsersStatsListBlockService * in your program root. It may just be loaded by the autoloader, in which case no changes are needed. But if you're loading it somewhere else, you may need to fix the arguments. Commented Oct 17, 2019 at 17:31

1 Answer 1

0

So all you need to do is to add the booking repository to the service definition.

# 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 }

The assumption here is that you also have the cocorico_report.booking.repository service already defined. If the repository is something you added then you will have to find the user repository service definition and basically clone it.

There is a lot more information available on how to explicitly configure services . Just try to avoid confusing it with the autowire approach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.