ContextErrorException:Catchable Fatal Error:传递给__construct()的参数1必须是Doctrine \ ORM \ EntityManager的实例,没有给出

时间:2014-10-29 12:39:16

标签: symfony dependency-injection

我在自定义类中注入doctrine服务时遇到了问题。 我正在使用symfony 2.3。我一直在尝试,但我仍然无法找到问题所在和缺失的地方。 以下是我的代码:

config.yml

imports:
   - { resource: parameters.yml }
   - { resource: security.yml }
   - { resource: "@ClCommonBundle/Resources/config/services.yml" }

services.yml:

parameters: 
    cl.pager.class: Cl\CommonBundle\Helpers\Pager

services:
    cl.pager:
      class: cl\CommonBundle\Helpers\Pager
      arguments: ["@doctrine.orm.entity_manager"]

pagerinterface.php

<?php

namespace Cl\CommonBundle\Helpers;

interface pagerInterface {

    public function getTotalRows($params);
}?>

pager.php:

<?php

namespace Cl\CommonBundle\Helpers;

use Cl\CommonBundle\Helpers\pagerInterface;
use Doctrine\ORM\EntityManager;

/**
 * Description of pagination
 */
class pager implements pagerInterface {

    protected $em;

    public function __construct(EntityManager $em) {
        $this->em = $em;
    }

    public function getTotalRows($params) {
        return $this->em->getRepository($params['repository'])->getActiveRowsCount($params);
    }

}?>

Citycontroller.php     

namespace Cl\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Cl\CommonBundle\Form\CityType;
use Cl\CommonBundle\Entity\City;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Cl\CommonBundle\Helpers\pager;

class CityController extends Controller {

    public function listAction(Request $request, $trigger = "next", $page = 1, $country, $cityname, $isactive) {

        $pager = new pager();

    }

}?>

错误

Whoops, looks like something went wrong.
1/1 ContextErrorException: Catchable Fatal Error: Argument 1 passed to Cl\CommonBundle\Helpers\pager::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /opt/lampp/htdocs/Cl/src/Cl/AdminBundle/Controller/CityController.php on line 117 and defined in /opt/lampp/htdocs/Cl/src/Cl/CommonBundle/Helpers/pager.php line 17

in /opt/lampp/htdocs/Cl/src/Cl/CommonBundle/Helpers/pager.php line 17
at ErrorHandler->handle('4096', 'Argument 1 passed to Cl\CommonBundle\Helpers\pager::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /opt/lampp/htdocs/Cl/src/Cl/AdminBundle/Controller/CityController.php on line 117 and defined', '/opt/lampp/htdocs/Cl/src/Cl/CommonBundle/Helpers/pager.php', '17', array()) in /opt/lampp/htdocs/Cl/src/Cl/CommonBundle/Helpers/pager.php line 17
at pager->__construct() in /opt/lampp/htdocs/Cl/src/Cl/AdminBundle/Controller/CityController.php line 117
at CityController->listAction(object(Request), 'next', '4', 'all', 'all', 'all')
at call_user_func_array(array(object(CityController), 'listAction'), array(object(Request), 'next', '4', 'all', 'all', 'all')) in /opt/lampp/htdocs/Cl/app/bootstrap.php.cache line 2889
at HttpKernel->handleRaw(object(Request), '1') in /opt/lampp/htdocs/Cl/app/bootstrap.php.cache line 2863
at HttpKernel->handle(object(Request), '1', true) in /opt/lampp/htdocs/Cl/app/bootstrap.php.cache line 2992
at ContainerAwareHttpKernel->handle(object(Request), '1', true) in /opt/lampp/htdocs/Cl/app/bootstrap.php.cache line 2272
at Kernel->handle(object(Request)) in /opt/lampp/htdocs/Cl/web/app_dev.php line 29


Stack Trace

in /opt/lampp/htdocs/Cl/vendor/symfony/symfony/src/Symfony/Component/Debug/ErrorHandler.php at line 162  -
                        }
                    });
                    throw new DummyException();
                }
            }
at ErrorHandler ->handle ('4096', 'Argument 1 passed to Cl\CommonBundle\Helpers\pager::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /opt/lampp/htdocs/Cl/src/Cl/AdminBundle/Controller/CityController.php on line 117 and defined', '/opt/lampp/htdocs/Cl/src/Cl/CommonBundle/Helpers/pager.php', '17', array())
in /opt/lampp/htdocs/Cl/src/Cl/CommonBundle/Helpers/pager.php at line 17  +
at pager ->__construct ()
in /opt/lampp/htdocs/Cl/src/Cl/AdminBundle/Controller/CityController.php at line 117  +
at CityController ->listAction (object(Request), 'next', '4', 'all', 'all', 'all')
at call_user_func_array (array(object(CityController), 'listAction'), array(object(Request), 'next', '4', 'all', 'all', 'all'))
in kernel.root_dir/bootstrap.php.cache at line 2889  +
at HttpKernel ->handleRaw (object(Request), '1')
in kernel.root_dir/bootstrap.php.cache at line 2863  +
at HttpKernel ->handle (object(Request), '1', true)
in kernel.root_dir/bootstrap.php.cache at line 2992  +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
in kernel.root_dir/bootstrap.php.cache at line 2272  +
at Kernel ->handle (object(Request))
in /opt/lampp/htdocs/Cl/web/app_dev.php at line 29  +

Logs  - 1 error

DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
INFO - Matched route "_admin_city_list" (parameters: "_controller": "Cl\AdminBundle\Controller\CityController::listAction", "trigger": "next", "page": "4", "country": "all", "cityname": "all", "isactive": "all", "_route": "_admin_city_list")
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
DEBUG - Read SecurityContext from the session
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
DEBUG - Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException".
DEBUG - Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException".
DEBUG - Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException".
CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\DummyException: "" at /opt/lampp/htdocs/Cl/vendor/symfony/symfony/src/Symfony/Component/Debug/ErrorHandler.php line 162
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".

3 个答案:

答案 0 :(得分:0)

在您的控制器中将$pager = new pager();替换为$pager = $this->get('cl.pager'); 这将调用service container加载您的服务并将objectManager注入构造函数

答案 1 :(得分:0)

您必须从容器获取服务,而不是通过创建新实例

namespace Cl\AdminBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Cl\CommonBundle\Form\CityType;
    use Cl\CommonBundle\Entity\City;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\JsonResponse;
    use Cl\CommonBundle\Helpers\pager;

    class CityController extends Controller {

        public function listAction(Request $request, $trigger = "next", $page = 1, $country, $cityname, $isactive) {

            $pager = $this->get('cl.pager'); //

        }

    }?>

答案 2 :(得分:0)

问题在于您实例化并调用寻呼机助手:

$pager = new pager();

你要让容器创建你的寻呼机助手的实例,你要创建一个扩展来做到这一点:

然后,您将能够在控制器内呼叫您的助手,如下所示,

$this->get('cl.pager');