ZF3控制器中的Doctrine EntityManager

时间:2016-09-06 07:33:06

标签: php doctrine zf3

请帮我解决ZendFramework中的以下问题。

我是ZF和PHP7的新手。 有几天我没能在控制器中使用Doctrine EntityManager。

我有:

我的控制器

namespace Sonun\Controller;

use Zend\Mvc\Controller\AbstractActionController,
    Doctrine\ORM\EntityManager;

class IndexController extends AbstractActionController
{
    protected $entityManager;

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

    namespace Sonun\Controller;

use Sonun\Controller\IndexController,
    Zend\ServiceManager\FactoryInterface,
    Zend\ServiceManager\ServiceLocatorInterface;

class IndexControllerFactory implements FactoryInterface
{

    public function createService(ServiceLocatorInterface $sm)
    {
        $entityManager = $sm->get("Doctrine\ORM\EntityManager");
        return new IndexController($entityManager);
    }
}

module.config.php

return [

    "controllers" => [
        "invokables" => [
            "Sonun\Controller\IndexController" => "Sonun\Controller\IndexController"
        ]
    ],

    "router" => [
        "routes" => [
            "sonun" => [
                "type" => "segment",
                "options" => [
                    "route" => "/sonun/[:action/][:id/]",
                    "constraints" => [
                        "action" => "[a-zA-Z0-9_-]*",
                        "id" => "[0-9]*"
                    ],
                    "defaults" => [
                        "controller" => "Sonun\Controller\IndexController",
                        "action" => "index"
                    ]
                ]
            ]
        ]
    ],

    "view_manager" => [
        "template_path_stack" => [
            __DIR__."/../view"
        ]
    ],

    "service_manager" => [
        "factories" => [
            "Sonun\Controller\IndexController" => "Sonun\Controller\IndexControllerFactory"
        ]
    ]
]

错误

     Fatal error: Uncaught TypeError: Argument 1 passed to ZendDeveloperTools\Exception\SerializableException::__construct() must be an instance of Exception, instance of TypeError given, called in C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Collector\ExceptionCollector.php on line 45 and defined in C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Exception\SerializableException.php:26 Stack trace: #0 C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Collector\ExceptionCollector.php(45): ZendDeveloperTools\Exception\SerializableException->__construct(Object(TypeError)) #1 C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Profiler.php(210): ZendDeveloperTools\Collector\ExceptionCollector->collect(Object(Zend\Mvc\MvcEvent)) #2 C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Listener\ProfilerListener.php(93): ZendDeveloperTools\Profiler->collect(Object(Zend\Mvc\MvcEvent)) #3 C:\xampp\htdocs\sonun\vendor\zendframework\zend-eventmanag in C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Exception\SerializableException.php on line 26

2 个答案:

答案 0 :(得分:1)

我也是ZF3的新手(不是每个人都有?)但我会接受一个镜头。你的工厂类需要看起来像     

namespace Application\Controller\Factory;

use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
use Application\Controller\IndexController;

class IndexControllerFactory implements FactoryInterface {

    public function __invoke(ContainerInterface $container, $requestedName, Array $options = null) {    
        $entityManager = $container->get('doctrine.entitymanager.orm_default');
        return new IndexController($entityManager);
    }

}

请注意,在ZF3中,您的工厂现在必须实施Zend\ServiceManager\Factory\FactoryInterface,即实施__invoke()而不是createService()

在您的module.config.php中,您的Controller不是可调用的 - 它对$entityManager有很强的依赖性,对吧? - 所以你需要取消它并用

之类的东西替换
'controllers' => [
    'factories' => [
       Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class,
    ],
],
祝你好运!

答案 1 :(得分:0)

您看到的特定错误由ZendDeveloperTools发出,并在其1.1.1版本中修复。运行composer update zendframework/zend-developer-tools即可获得它。

但是,它不是问题的根源;该模块只是试图报告异常,在这种情况下是一个类型错误;你必须从那里继续调试。

最后,据我所知,DoctrineModule(及相关模块)尚未与ZF的v3版本兼容。您可能需要切换到v2,直到他们参与迁移。