我正在将当前在ZendFramework1(ZF1)上运行的应用程序升级到ZendFramework2(ZF2)。我无法让数据结果从ZF2连接返回。
在ZF1中,此测试完美无缺:
$db = Zend_Db::factory('Pdo_Mssql', array(
'host' => 'ServerNameFromFreeTdsConfig',
'charset' => 'UTF-8',
'username' => 'myUsername',
'password' => 'myPassword',
'dbname' => 'database_name',
'pdoType' => 'dblib'
));
$stmt = $db->prepare("select * from Products");
$stmt->execute();
$result = $stmt->fetchAll();
$stmt->closeCursor();
然而,我一直在ZF2尝试这个,但我真的没有到达任何地方。在我的config \ autoload \ global.php中,我有:
return array(
'db' => array(
'host' => 'ServerNameFromFreeTdsConfig',
'charset' => 'UTF-8',
'dbname' => 'database_name',
'username' => 'myUsername',
'password' => 'myPassword',
'driver' => 'pdo',
'pdodriver' => 'dblib',
),
);
在Module.php文件中:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$config = $e->getApplication()->getServiceManager()->get('Configuration');
$dbAdapter = new Adapter($config['db'], new SqlServer());
GlobalAdapterFeature::setStaticAdapter($dbAdapter);
}
然后在Model \ Products.php
中class Products extends AbstractTableGateway
{
protected $table;
protected $featureSet;
public function __construct($table = 'Products') {
$this->table = $table;
$this->featureSet = new FeatureSet();
$this->featureSet->addFeature(new GlobalAdapterFeature());
$this->initialize();
}
//Test the connection.
public function getProducts() {
$result = $this->getAdapter()->query("select * from Products", Adapter::QUERY_MODE_EXECUTE);
die(var_dump($result));
}
}
看起来它正在连接,因为上面的“var_dump”返回一个[“fieldCount”:protected] => int(7)是正确的(该表中有7列)。但是,它没有返回任何结果。 为了让它在ZF2中运行,我需要做些什么?我是否需要使用ZF1 Zend_Db_Adapter_Pdo_Mssql.php文件中的代码以某种方式扩展Zend \ Db \ Adapter \ Adapter?还是有一些我想念的简单解决方案?
感谢您的任何见解。
答案 0 :(得分:0)
我认为您不需要提及用户名和密码
resources.db.adapter = "sqlsrv"
resources.db.host = "localhost\SQLEXPRESS"
resources.db.dbname = "DatabaseName"
resources.db.isDefaultTableAdapter = true
resources.db.driver_options.ReturnDatesAsStrings = true
答案 1 :(得分:0)
我最终编写了自己在我工作的Zend Framework 1适配器中采用的适配器。如果有人遇到这篇文章寻找同一问题的解决方案,并希望我转置的代码副本,请告诉我。有很多代码,或者我会在这里发布。