2012-01-23 50 views
0

的結果集獲得元數據我有一個代碼在PHP爲:從Zend框架

$result = mysql_query($cxn,$sql_query) 
$dataset= mysqli_fetch_fields($result); 
for ($i = 0; $i < 13; $i++) { 
     $dataset[$i]->name 
} 

我想在Zend的上面的代碼。這個mysql_fetch_fields($ result)返回關於給定結果集$ result的字段信息?如何在zend框架中完成它?我已經使用了它,我發現我們可以從一個特定的表中檢索有關colums的信息但從結果集如何在zend框架中檢索?

回答

1

目前在Zend Framework中是不可能的。看看Request solution for result set metadata。你可以嘗試使用實驗PDOStatement::getColumnMeta

UPDATE - 例如代碼中的註釋

sample table structure 
table1: id (int), field1 char(3) 
table2: id (int), field2 char(3) 

<?php 

require_once('Zend/Loader/Autoloader.php'); 

$autoloader = Zend_Loader_Autoloader::getInstance(); 

// create MySQL database adapter 
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'  => '127.0.0.1', 
    'username' => 'test', 
    'password' => 'test', 
    'dbname' => 'test' 
)); 

// create temporary table 
$result = $db->getConnection()->exec(' 
    CREATE TEMPORARY TABLE myTable 
    SELECT 
     t1.id, 
     t1.field1, 
     t2.field2 
    FROM table1 t1 
     INNER JOIN table2 t2 
      ON t1.id = t2.id 
'); 

// describe 
$info = $db->describeTable('myTable'); 

var_dump($info); 

// drop table 
$result = $db->getConnection()->exec('DROP TEMPORARY TABLE myTable'); 

More about running "other" database statements

+0

那right.Above方法沒有worki ng.So上面的代碼用PHP編寫的代碼是什麼?因爲我必須從查詢中返回字段名稱。 – ryan

+0

您可以嘗試使用mysql適配器並使用Bill Karwin的建議http://framework.zend.com/issues/browse/ZF-745?focusedCommentId=27931&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel# comment-27931 CREATE TEMPORARY TABLE foo SELECT ...上面的查詢... DESCRIBE foo;' – BartekR

+0

Thanks for reply.What應該是語法,如果我想創建臨時表作爲從基表的選擇查詢? – ryan

0

我發現越來越關於表中的所有元數據是非常簡單的與Zend框架。

  • 首先創建一個DbTable模型表格:Define Table Class
  • 實例化對象:$table = new Application_Model_DbTable_Table();
  • 電話 - >該對象信息():$元= $表 - >信息()

下面是使用這種方法的一個轉儲的摘錄:Retrieving Table Metadata

array(10) { 
    ["schema"] => NULL 
    ["name"] => string(5) "track" 
    ["cols"] => array(6) { 
    [0] => string(7) "trackid" 
    [1] => string(9) "weekendid" 
    [2] => string(7) "shiftid" 
    [3] => string(13) "bidlocationid" 
    [4] => string(3) "qty" 
    [5] => string(4) "lead" 
    } 
    ["primary"] => array(1) { 
    [1] => string(7) "trackid" 
    } 
    ["metadata"] => array(6) { 
    ["trackid"] => array(14) { 
     ["SCHEMA_NAME"] => NULL 
     ["TABLE_NAME"] => string(5) "track" 
     ["COLUMN_NAME"] => string(7) "trackid" 
     ["COLUMN_POSITION"] => int(1) 
     ["DATA_TYPE"] => string(8) "smallint" 
     ["DEFAULT"] => NULL 
     ["NULLABLE"] => bool(false) 
     ["LENGTH"] => NULL 
     ["SCALE"] => NULL 
     ["PRECISION"] => NULL 
     ["UNSIGNED"] => NULL 
     ["PRIMARY"] => bool(true) 
     ["PRIMARY_POSITION"] => int(1) 
     ["IDENTITY"] => bool(true) 
    } 
... cont ... 
} 
    ["rowClass"] => string(27) "Application_Model_Row_Track" 
    ["rowsetClass"] => string(20) "Zend_Db_Table_Rowset" 
    ["referenceMap"] => array(3) { 
    ["Weekend"] => array(3) { 
     ["columns"] => string(9) "weekendid" 
     ["refTableClass"] => string(33) "Application_Model_DbTable_Weekend" 
     ["refColumns"] => string(9) "weekendid" 
    } 
    ["Shift"] => array(3) { 
     ["columns"] => string(7) "shiftid" 
     ["refTableClass"] => string(31) "Application_Model_DbTable_Shift" 
     ["refColumns"] => string(7) "shiftid" 
    } 
    ["BidLocation"] => array(3) { 
     ["columns"] => string(13) "bidlocationid" 
     ["refTableClass"] => string(37) "Application_Model_DbTable_BidLocation" 
     ["refColumns"] => string(13) "bidlocationid" 
    } 
    } 
    ["dependentTables"] => array(1) { 
    [0] => string(32) "Application_Model_DbTable_Member" 
    } 
    ["sequence"] => bool(true) 
}