1

Please, help me to solve the following problem in ZendFramework.

I'm new in ZF and PHP7. For a few days I havent been able to use Doctrine EntityManager in controller.

I have:

My Controller

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

Factory

    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"
        ]
    ]
]

Error

     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
Baisar
  • 11
  • 1
  • 3

2 Answers2

1

I too am new to ZF3 (isn't everyone?) but I will take a shot. Your factory class needs to look something like

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);
    }

}

Note that in ZF3 your factories now have to implement the Zend\ServiceManager\Factory\FactoryInterface, i.e., implement __invoke() rather than createService().

In your module.config.php, your Controller is not an invokable -- it has a hard dependency on that $entityManager, right? -- so you need to do away with that and replace with something like

'controllers' => [
    'factories' => [
       Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class,
    ],
],

Good luck!

David
  • 815
  • 8
  • 18
0

The particular error you are seeing is emitted by ZendDeveloperTools, and fixed in its 1.1.1 release. Run composer update zendframework/zend-developer-tools to get it.

However, it's not the root of the problem; that module is simply trying to report an exception, in this case a type error; you'll have to keep debugging from there.

Finally, the DoctrineModule (and related modules) are, to my knowledge, not yet compatible with the v3 releases of ZF. You may need to switch to v2 until they compete their migration.

weierophinney
  • 1,790
  • 9
  • 17
  • Thank you very much Matthew. I appreciate your answer. I will try to solve. – Baisar Sep 10 '16 at 15:51
  • If you have time, please write how should I write my factory. Thanks. Yours sincerely – Baisar Sep 10 '16 at 16:34
  • DoctrineORM works on ZF3. But I cant use DoctrineEntityManager via factory. Now I get: Argument 1 passed to Sonun\Controller\IndexController::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in C:\xampp\htdocs\sonun\vendor\zendframework\zend-servicemanag‌​er\src\Factory\Invok‌​ableFactory.php on line 32 – Baisar Sep 10 '16 at 16:37
  • that's because you have the controller registered as an invokable when you need a factory. see my answer below/ – David Oct 17 '16 at 14:37