从zend框架中的结果集中获取元数据

时间:2012-01-23 13:20:31

标签: php zend-framework

我在php中有一段代码:

$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框架中执行?我用Google搜索了它,我发现我们可以从特定的表中检索有关colums的信息但是从结果集如何在zend框架中检索?

2 个答案:

答案 0 :(得分:1)

目前Zend Framework无法实现。看看Request solution for result set metadata。您可以尝试使用实验PDOStatement::getColumnMeta

更新 - 评论中代码的示例

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

答案 1 :(得分:0)

我发现使用Zend Framework可以非常简单地获取有关表的所有元数据。

  • 首先为您的表创建一个DbTable模型:Define Table Class
  • 实例化对象:$table = new Application_Model_DbTable_Table();
  • 该对象的
  • call - &gt; info():$ meta = $ table-&gt; info()

以下是使用此方法的转储摘录: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)
}