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!

4

2 回答 2

0

几周前我刚刚启动了我的第一个 zf3 应用程序,我遇到了同样的问题。

我所做的是,在当前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'
            ]
        ]
    ]
],

如您所见,我定义了Admin_driver并将其加载到orm_default中。

于 2018-06-22T04:07:04.777 回答
0

好吧,经过几次尝试,我找到了答案。例如,如果您更喜欢使用 Panel\Model\Chat 和 Panel\Model\ChatTable 而不是 Application\Model\Chat 和 Application\Model\ChatTable,则配置应如下所示。

在您的 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
                );
            },
        ],
    ];
}

然后,在您的 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
        ]);
    }

}
于 2016-09-07T13:40:25.687 回答