从其他模块zf3访问模型


Access models from other modules zf3

我试图配置ZF3与ZF2几个项目后,但无法访问一个模型是在另一个模块。

我在我的数据库中有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)
                );
            },
        ],
    ];
}

然后,我可以在我的应用程序'控制器'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(),
    ]);
}

}

这工作完美。我的问题是:如果,例如,我更喜欢把聊天和ChatTable模型放在另一个模块中,例如,在面板'模型'ChatTable下,并从我的应用程序模块访问它们?我应该做什么改变?

在ZF2中,使用服务定位器很容易做到这一点。我发现了一个建议使用服务工厂的问题,但是,至少在我的情况下,并没有解决在模块内和模块外同时使用模型的想法。

提前感谢。再见!

嗯,经过几次尝试,我找到了答案。例如,如果您更喜欢使用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
        ]);
    }
}

我几周前才开始我的第一个zf3应用程序,我和你有同样的问题。

我所做的是,在当前module.config.php 中定义origin模块的驱动程序。
'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中。