从模型访问 Zend Framework 2 中的数据库适配器


Access Database Adapter in Zend Framework 2 from a Model

我是Zf2的新手,我想知道如何从模型访问Zend Framework 2中的数据库适配器?如果我在模型中工作,并且我想获取数据库适配器?如何?

我试过了

use Zend'Db;
use Zend'Db'TableGateway'TableGateway;
use Zend'Db'Adapter'Adapter;
$dbAdapter = $this->getServiceLocator()->get('Zend'Db'Adapter'Adapter');
$adapter = new Adapter(); 

但不起作用!如果你给我一些例子,我会很高兴.

塔瑞克

您无法以这种方式从模型访问服务定位器。而且您不需要为此目的。如果您的模块.php当前模型的设置正确,请使用:

$adapter = $this->tableGateway->getAdapter();