访问其他模块zf3的模型

时间:2016-09-02 16:29:28

标签: php zend-framework zf3

我正在尝试使用ZF2在一些项目之后配置ZF3,但无法访问另一个模块中的模型。

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

然后,我可以在我的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很容易。我发现了一个建议使用服务工厂的问题,但至少在我的情况下,并没有解决在模块内部和模块外部同时使用模型的想法。

提前致谢。再见!

2 个答案:

答案 0 :(得分: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
        ]);
    }

}

答案 1 :(得分:0)

几周前,我刚刚启动了我的第一个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 中。