Page 1 of 1

Integrating doctrine, factory

Posted: Tue Nov 22, 2016 4:41 pm
by fern3379
Hi guys,

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:

Code: Select all

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

Code: Select all

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:

on module.config.php

Code: Select all

 '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.

Code: Select all

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:

snag. gy /tF93uB.jpg

Thanks!

Re: Integrating doctrine, factory

Posted: Mon Dec 05, 2016 12:07 pm
by stij2324
Wich version of the framework are you sing zf2 or zf3?

Re: Integrating doctrine, factory

Posted: Fri Dec 09, 2016 2:25 pm
by fern3379
ZF2.. But I figured it out already! Thanks!