2012-01-23 52 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) 
}