获取表模型中的默认zend数据库适配器 [英] Get default zend db adapter in table model

查看:82
本文介绍了获取表模型中的默认zend数据库适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在表模型中获取默认的数据库适配器?我想用它来创建交易.

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

在database.global.php中:

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\''
        ),
    ),
);

现在我想拥有 我的albumTable.php中的$this->adapter

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');    
    }

但是我得到了错误:

致命错误:类Ajax \ Model \ AlbumTable包含2个抽象方法 因此必须声明为抽象或实施其余的 方法 (Zend \ ServiceManager \ ServiceLocatorAwareInterface :: setServiceLocator, Zend \ ServiceManager \ ServiceLocatorAwareInterface :: getServiceLocator) 在

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

推荐答案

添加以下功能:

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');

但是,如果您阅读入门指南解释了如何使用服务管理器工厂构造TableGateways,并传入DbAdapter和其他参数,例如:

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);
},

这篇关于获取表模型中的默认zend数据库适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆