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!