Get default zend db adapter in table model

岁酱吖の 提交于 2019-12-25 03:59:34

问题


How can I get the default db adabter in my table model? I want to use it to create a transaction.

In database.global.php:

    return array(
   'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
        'aliases' => array(
            'db' => 'Zend\Db\Adapter\Adapter',
        ),
    ),
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=cww;host=localhost',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
);

Now I would like to have $this->adapter in my albumTable.php

I tried to receive it as follow:

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Update;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Ajax\Model\Album;

class AlbumTable implements ServiceLocatorAwareInterface
{
    protected $tableGateway;
    protected $adapter;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
        $this->adapter  = $this->getServiceLocator()->get('db');    
    }

But I get the error:

Fatal error: Class Ajax\Model\AlbumTable contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator, Zend\ServiceManager\ServiceLocatorAwareInterface::getServiceLocator) in


回答1:


Add the following functions:

public function getServiceLocator() {
    return $this->serviceLocator;
}


public function setServiceLocator(Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) {
    $this->serviceLocator= $serviceLocator;
    return $this;
}

Then you can do:

$this->$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');

However, if you read the getting started guide it explains how to construct TableGateways using a service manager factory, passing in the DbAdapter and other parameters like so:

'RoleTableGateway' => function ($sm) {
    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
    $resultSetPrototype = new ResultSet();
    $resultSetPrototype->setArrayObjectPrototype(new Role());
    return new TableGateway('role', $dbAdapter, null, $resultSetPrototype);
},



回答2:


use this to create a database adapter:

$adapter = $this->getServiceLocator()->get('db');

you can use 'db' only as you have created alias. You can also try writing things in locals.php in \autoload\config\local.php. Now, suppose we want to execute a database query in mySQL: Create a sql statetment and put in variable $sql. Now do this:

$statement = $adapter->createStatement($sql);
$result = $statement->execute();

Hope this helps.



来源:https://stackoverflow.com/questions/15413923/get-default-zend-db-adapter-in-table-model

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