0
votes

I use zend on my company but i have never started setting it up. I downloaded the framework and want to integrate doctrine... I tried to get the doctrine object using getServiceLocator() but on zend 2x it will be deprecated and when I try to do this:

 public function indexAction()
 {
     $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
 }

I get the following exceptions:

1 - An exception was raised while creating "Doctrine\ORM\EntityManager"; no instance returned 2 - An abstract factory could not create an instance of doctrine.entitymanager.ormdefault(alias: doctrine.entitymanager.orm_default).

So I tried to pass the doctrine object by factory... but the factory is never called. That's what I did:

 'controllers' => array(
        'invokables' => array(
            'Album\Controller\Album' => 'Album\Controller\AlbumController'
        ),
        'factories' => [
            'Album\Controller\Album' => 'Album\Controller\AlbumControllerFactory'
        ]
    ),

On module.php

public function getControllerConfig()
    {
        return [
            'factories' => [
                '\Album\Controller\Album' => function() {
                    exit;
                }
            ]
        ];
    }

Nothing that I do seems to get inside the factory class.

class AlbumControllerFactory implements FactoryInterface
{
    public function __construct()
    {
        exit;
    }
    public function createService(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) {
        exit;
        /* @var $serviceLocator \Zend\Mvc\Controller\ControllerManager */
        $sm   = $serviceLocator->getServiceLocator();
        $em = $sm->get('Doctrine\ORM\EntityManager');
        $controller = new AlbumController($em);
        return $controller;
    }
}

class AlbumController extends AbstractActionController
{
    public function indexAction()
    {
        $em = $this->getServiceLocator()
            ->get('Doctrine\ORM\EntityManager');

Here is how my structure looks like:

enter image description here

Thanks!

1

1 Answers

1
votes

The mapping of your classes are wrong for your AlbumController as you use Album\Controller\Album instead of Album\Controller\AlbumController. Use the FQCN (Fully Qualified Class Name).

For your module.php, there is a '\' infront of the class name, aswell you forgot the Controller at the end.

use Album\Controller\AlbumController;
use Album\Controller\AlbumControllerFactory;

class Module implements ControllerProviderInterface
{
    /**
     * Expected to return \Zend\ServiceManager\Config object or array to seed
     * such an object.
     * @return array|\Zend\ServiceManager\Config
     */
    public function getControllerConfig()
    {
        return [
            'aliases' => [
                'Album\Controller\Album' => AlbumController::class,
            ],
            'factories' => [
                AlbumController::class => AlbumControllerFactory::class,
            ]
        ];
    }