sql-server - 通过 Linux 从 Zend2 访问 MSSQL

标签 sql-server linux zend-framework2

我正在将当前在 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?或者我缺少一些简单的解决方案?

感谢您的见解。

最佳答案

我认为您不需要提及用户名和密码

resources.db.adapter = "sqlsrv"
resources.db.host = "localhost\SQLEXPRESS"
resources.db.dbname = "DatabaseName"
resources.db.isDefaultTableAdapter = true 
resources.db.driver_options.ReturnDatesAsStrings = true

关于sql-server - 通过 Linux 从 Zend2 访问 MSSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14758519/

相关文章:

sql - 从SQL中的一列字符串中获取最常用的单词

linux - 在没有互联网连接的情况下无法使用 yum 安装 httpd-devel rpm

php - 如何将选项/参数传递给 ZendFramework 2 中的 formCollection 字段集?

php - Zend框架2 : How to write custom query using tablegateway

sql - 在事务中包装插入对 Sql Server 的性能有多大帮助?

sql-server - 将 Azure SQL 数据库镜像到本地 SQL Server

linux - 通过输入重定向 (<) 将参数传递给 Bash Shell 脚本

mysql - Zend Framework 2 Db getColumns

Python:将数据批量插入 SQL Server

linux - 将应用程序从 Linux 移植到 Windows