How to get Zend\Db\Adapter instance from within a Model? (ZF2)

天大地大妈咪最大 提交于 2019-12-03 11:19:50

问题


I'm creating abstract models for managing database entities - I already have EntityAbstract, EntitySetAbstract and a ManagerAbstract models. In my ManagerAbstract model I need a Zend/Db/Adapter instance in order to create a Zend\Db\TableGateway.

How could I pull the main instance of the adapter to my ManagerAbstract? In ZF1 I could have achieved this with Zend_Registry.

If this isn't the right way of doing things in ZF2, I would love to hear the correct way to this kind of things.

Thanks!


回答1:


Use the Dependency Injection Container, Zend\Di. The ZfcUser project does this if you want to poke around in some working code.

Alternatively, the basic approach is something like this (code untested!):

Firstly: configure the DI to inject the database connection information:

config/autoload/local.config.php:

<?php
return array(
    'di' => array(
        'instance' => array(
        'Zend\Db\Adapter\Adapter' => array(
                'parameters' => array(
                    'driver' => 'Zend\Db\Adapter\Driver\Pdo\Pdo',
                ),
            ),
            'Zend\Db\Adapter\Driver\Pdo\Pdo' => array(
                'parameters' => array(
                    'connection' => 'Zend\Db\Adapter\Driver\Pdo\Connection',
                ),
            ),
            'Zend\Db\Adapter\Driver\Pdo\Connection' => array(
                'parameters' => array(
                    'connectionInfo' => array(
                        'dsn'            => "mysql:dbname=mydatabasename;host=localhost",
                        'username'       => 'myusername',
                        'password'       => 'mypassword',
                        'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''),
                    ),
                ),
            ),
        ),
    ),
);

Secondly, within your module's module.config.php file, inject the adapter into the mapper:

module/My/config/module.config.php:

<?php
return array(
    'di' => array(

            // some config info...

            'My\Model\ManagerAbstract' => array(
                'parameters' => array(
                    'adapter'  => 'Zend\Db\Adapter\Adapter',
                ),
            ),

            // more config info...
    )
);

Finally, ensure that your ManagerAbstract class can receive the injection:

module/My/src/My/Model/ManagerAbstract.php:

<?php
namespace My\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterAwareInterface;

abstract class ManagerAbstract implements AdapterAwareInterface 
{
    /**
     * @var Zend\Db\Adapter\Adapter
     */
    protected $adapter;

    // some code 

    public function setDbAdapter(Adapter $adapter)
    {
        $this->adapter = $adapter;
    }

    // some more code
}

Note that to use any sub-class, you need to retrieve it via the DIC or inject the mapper into the service and then inject the service into the controller (or other service) where you want to use it.



来源:https://stackoverflow.com/questions/10254574/how-to-get-zend-db-adapter-instance-from-within-a-model-zf2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!