0

I'm trying to configure ZF3 after a few projects with ZF2, but cannot access a model which is in another module.

I have 3 tables in my database, and I defined the gateways as follow in my Application\src\Module.php

public function getServiceConfig()
{
    return [
        'factories' => [
            Model\ChatTable::class => function($container) {
                $tableGateway = $container->get(Model\ChatTableGateway::class);
                return new Model\ChatTable($tableGateway);
            },
            Model\ChatTableGateway::class => function ($container) {
                $dbAdapter = $container->get(AdapterInterface::class);
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Model\Chat());
                return new TableGateway('chat', $dbAdapter, null, $resultSetPrototype);
            },
            Model\OperadorTable::class => function($container) {
                $tableGateway = $container->get(Model\OperadorTableGateway::class);
                return new Model\OperadorTable($tableGateway);
            },
            Model\OperadorTableGateway::class => function ($container) {
                $dbAdapter = $container->get(AdapterInterface::class);
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Model\Operador());
                return new TableGateway('operador', $dbAdapter, null, $resultSetPrototype);
            },
            Model\ConversacionTable::class => function($container) {
                $tableGateway = $container->get(Model\ConversacionTableGateway::class);
                return new Model\ConversacionTable($tableGateway);
            },
            Model\ConversacionTableGateway::class => function ($container) {
                $dbAdapter = $container->get(AdapterInterface::class);
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Model\Conversacion());
                return new TableGateway('conversacion', $dbAdapter, null, $resultSetPrototype);
            },
        ],
    ];
}

public function getControllerConfig()
{
    return [
        'factories' => [
            Controller\IndexController::class => function($container) {
                return new Controller\IndexController(
                    $container->get(Model\ChatTable::class),
                    $container->get(Model\OperadorTable::class),
                    $container->get(Model\ConversacionTable::class)
                );
            },
        ],
    ];
}

Then, I can use them in my Application\Controller\IndexController as follow:

namespace Application\Controller;

use Application\Model\ChatTable;
use Application\Model\OperadorTable;
use Application\Model\ConversacionTable;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{

private $chatTable;
private $operadorTable;
private $conversacionTable;

//TABLAS

public function __construct(
    ChatTable $chatTable, 
    OperadorTable $operadorTable,
    ConversacionTable $conversacionTable
){
    $this->chatTable = $chatTable;
    $this->operadorTable = $operadorTable;
    $this->conversacionTable = $conversacionTable;
}

//VIEW ACTIONS

public function indexAction()
{
    return new ViewModel([
        'chats' => $this->chatTable->fetchAll(),
        'operadores' => $this->operadorTable->fetchAll(),
        'conversaciones' => $this->conversacionTable->fetchAll(),
    ]);
}


}

This works perfectly. My question is ¿what if, for example, I prefer to put the Chat and ChatTable model in another module, for example, under Panel\Model\ChatTable and acces them from my Application module? ¿what changes should I make?

In ZF2 this was easy using Service Locator. I have found a question suggesting the use of service factories, but, at least in my case, does not solve the idea of using at the same time models within the module and from outside the module.

Thanks in advance. Bye!

jairusgr
  • 137
  • 2
  • 9

2 Answers2

0

Well, I found the answer after a few attempts. For example, if you prefer to use Panel\Model\Chat and Panel\Model\ChatTable instead of Application\Model\Chat and Application\Model\ChatTable, the configuration should be the following.

In your Application\src\Module.php:

public function getServiceConfig()
{
    return [
        'factories' => [
            \Panel\Model\ChatTable::class => function($container) {
                $tableGateway = $container->get(\Panel\Model\ChatTableGateway::class);
                return new \Panel\Model\ChatTable($tableGateway);
            },
            \Panel\Model\ChatTableGateway::class => function ($container) {
                $dbAdapter = $container->get(AdapterInterface::class);
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new \Panel\Model\Chat());
                return new TableGateway('chat', $dbAdapter, null, $resultSetPrototype);
            },

        ],
        //rest of stuff
    ];
}

public function getControllerConfig()
{
    return [
        'factories' => [
            Controller\IndexController::class => function($container) {
                return new Controller\IndexController(
                    $container->get(\Panel\Model\ChatTable::class),
                    //rest of stuff
                );
            },
        ],
    ];
}

Then, in your Application\Controller\IndexController:

use Panel\Model\ChatTable;
//rest of stuff
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{

    private $chatTable;
    //rest of stuff

    //TABLAS

    public function __construct(
        ChatTable $chatTable, 
        //rest of stuff

    ){
        $this->chatTable = $chatTable;
        //rest of stuff
    }

    //VIEW ACTIONS

    public function indexAction()
    {
        return new ViewModel([
            'chats' => $this->chatTable->fetchAll(),
            //rest of stuff
        ]);
    }

}
jairusgr
  • 137
  • 2
  • 9
0

I just started my first zf3 app weeks ago, and I had the same problem with you.

What did I do was, define the driver of the origin module inside the current module.config.php

'doctrine'           => [
    'driver' => [
        __NAMESPACE__ . '_driver' => [
            'class' => AnnotationDriver::class,
            'cache' => 'array',
            'paths' => [__DIR__ . '/../src/Model']
        ],
         'Admin_driver' => [
            'class' => AnnotationDriver::class,
            'cache' => 'array',
            'paths' => [__DIR__ . '/../../Admin/src/Model']
        ],
        'orm_default'             => [
            'drivers' => [
                __NAMESPACE__ . '\Model' => __NAMESPACE__ . '_driver',
                'Admin\Model'            => 'Admin_driver'
            ]
        ]
    ]
],

As you can see, I defined the Admin_driver and loaded it into the orm_default .

Hanif
  • 1