开发者

Extending Zend_Db

开发者 https://www.devze.com 2022-12-16 00:14 出处:网络
I apologize if my title is a bit misleading and it turns out that it\'s some other class under Zend_Db.

I apologize if my title is a bit misleading and it turns out that it's some other class under Zend_Db.

I use the following method of extracting data from a MSSQL:

// $_config contains information about how to connect to my MSSQL server
$config = new Zend_Config($_config);
$db = Zend_Db::factory($config->database);

$sql = "SELECT * FROM table1";
print_r($db->fetchAll($sql));

So far no problem and everything runs smooth :).

Now I need to run some more complex queries with multiple rowsets:

$sql2 = <<<EOD
  DECLARE @test IN开发者_如何学PythonT
  SET @test = 42
  SELECT * FROM table1 WHERE col1 = @test

  SELECT col2 FROM table2 UNION 
  SELECT col3 FROM table2 

  SELECT * FROM table3
EOD;
print_r($db->fetchAll($sql2));

I hope you get the idea.

Using $db->fetchAll($sql2); will result in

Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[HY000]: General error: 10038 Attempt to initiate a new SQL Server operation with results pending. [10038] (severity 7) [(null)]' in \Sacp026a\sebamweb$\prod\includes\Zend\Db\Statement\Pdo.php:234

The following function will return all the correct rowsets:

function sqlquery_multiple($zdb, $sql) {
    $stmt = $zdb->query($sql);
    $rowsets = array();
    do {
      if ($stmt->columnCount() > 0) {
        $rowsets[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
      }
    } while ($stmt->nextRowset());
    return $rowsets;
}
print_r(sqlquery_multiple($db, $sql2));

Now my big question is: How do I extend Zend_Db, so I can implement and use the function above as $db->fetchMulti($sql2); instead of sqlquery_multiple($db, $sql2)?

Thanks in advance :)

NB: It's worth mentioning that I'm using the ODBC-patch in order to be able to fetch multiple rowsets in the first place.

0

精彩评论

暂无评论...
验证码 换一张
取 消