2016-09-02 3 views
0

Я пытаюсь настроить ZF3 после нескольких проектов с ZF2, но не могу получить доступ к модели, находящейся в другом модуле.Модели доступа из других модулей zf3

У меня есть 3 таблицы в моей базе данных, и я определил шлюзы, как следовать в моем приложении \ 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) 
       ); 
      }, 
     ], 
    ]; 
} 

Тогда я могу использовать их в моем Application \ Controller \ IndexController, как следовать :

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(), 
    ]); 
} 


} 

Это прекрасно работает. Мой вопрос: что, если, например, я предпочитаю помещать модель Chat и ChatTable в другой модуль, например, в Panel \ Model \ ChatTable и присоединяется к ним из моего модуля приложения? Какие изменения я должен внести?

В ZF2 это было легко с помощью Service Locator. Я нашел вопрос, предлагающий использование сервисных заводов, но, по крайней мере, в моем случае, не решает идею использования в то же время моделей в модуле и вне модуля.

Заранее спасибо. До свидания!

ответ

0

Ну, я нашел ответ после нескольких попыток. Например, если вы предпочитаете использовать Panel \ Model \ Chat и Panel \ Model \ ChatTable вместо Application \ Model \ Chat и Application \ Model \ ChatTable, конфигурация должна быть следующей.

в вашем приложении \ 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 
     ]); 
    } 

} 
Смежные вопросы