douwang6635 2017-08-17 10:19
浏览 73

zend框架3,一个控制器中的多个db表

I have a ZF3 project and an index controller that accesses a single postgres table. I have the usual factories setup

return array(
        'factories' => [
            Model\IsdepotstockTable::class => function($container) {
                $tableGateway = $container->get(Model\IsdepotstockTableGateway::class);
                return new Model\IsdepotstockTable($tableGateway);
            },
            Model\IsdepotstockTableGateway::class => function ($container) {
                $dbAdapter = $container->get(AdapterInterface::class);
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Model\Isdepotstock());
                return new TableGateway('isdepotstock', $dbAdapter, null, $resultSetPrototype);
            },
        ],

I also have my controller constructor thus:

public function __construct(IsdepotstockTable $table)
{
    $this->isdepotstockTable = $table;
}

My question is, if I wish to access a second table, how do I modify the construct statement to handle multiple tables? Obviously I have to add the factories for the additional table that much I understand.

I've looked through the ZF3 documentation but cannot find any example.

Thanks

  • 写回答

1条回答 默认 最新

  • doubi4814 2017-08-17 10:32
    关注

    I believe you are looking for a controller factory function which will instantiate the controller and pass the arguments to the constructor.

    Add your second table class as the 2nd argument in the __construct() method, then create your factory in module.config.php.

    <?php
    use Zend\ServiceManager\Factory\InvokableFactory;
    
    return [
        // ...
    
        'controllers' => [
            'factories' => [
                Controller\IndexController::class => InvokableFactory::class
                // Put other controllers registration here
            ],
        ],
    
        // ...
    ];
    

    Here is a free open source book on ZF3, I link to the controller registration section for your reference. Good luck! https://olegkrivtsov.github.io/using-zend-framework-3-book/html/en/Model_View_Controller/Controller_Registration.html

    评论

报告相同问题?