如何在我的表模型中获取默认的db adabter?我想用它来创建一个事务。在表模型中获取默认的zend db适配器
在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\''
),
),
);
现在我想有 $this->adapter
我albumTable.php
我试着接受它如下:
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');
}
但我得到的错误:
个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
感谢您的回答。我已经设置了tableGetaway,所以我现在用这个来获取我需要的'$ this-> tableGateway-> getAdapter()';这是我所需要的:)谢谢反正 – directory 2013-03-14 16:59:46
我得到了这个错误“致命的错误:调用成员函数get()在一个非对象在F:\ xampp \ htdocs \ zendtest \模块\应用程序\ src \应用程序\模型\ Signup.php第41行“。请参阅我的问题”http://stackoverflow.com/questions/20319388/calling-adapter-in-model“,是您的解决方案适合我的情况? – user1844626 2013-12-02 03:41:01
您需要手动将服务定位器或数据库适配器设置到您的模型类中。对这些问题的评论应提供指导。 – 2013-12-02 08:54:19