Zend_Db_Table 根據主鍵查找數據 官方手冊錯誤

 官方手冊中介紹說:

 

根據主鍵查找數據

通過調用find()方法,可以使用主鍵值輕鬆地在表中檢索數據.假如你只想要查詢某 一條數據,該方法將回返回一個zend_db_table_row對象,而當你想要查詢多條記錄時 ,將會返回一個zend_db_table_rowset對象.


<?php
class RoundTable extends Zend_Db_Table {}

$table = new RoundTable();

// SELECT * FROM round_table WHERE id = "1"
$row = $table->find(1);

// SELECT * FROM round_table WHERE id IN("1", "2", 3")
$rowset = $table->find(array(1, 2, 3));
?>
 其實       

" 假如你只想要查詢某 一條數據,該方法將回返回一個zend_db_table_row對象"錯誤的,經測試返回的也是一個"zend_db_table_rowset對象":

代碼:

<?php

require_once 'Zend/Db.php';

$params = array(

    'host' => '127.0.0.1',

    'username' => 'root',

    'password' => '',

    'dbname' => 'guestbook'

);

$db = Zend_Db::factory('PDO_MYSQL', $params);

// 爲所有的Zend_Db_Table對象設定默認的adapter

require_once 'Zend/Db/Table.php';

Zend_Db_Table::setDefaultAdapter($db);

class Guestbook extends Zend_Db_Table {    

}

$table = new Guestbook();

// SELECT * FROM round_table WHERE id = "1"

$row = $table->find(1);

var_dump($row);

// SELECT * FROM round_table WHERE id IN("1", "2", 3")

$rowset = $table->find(array(1, 2, 3));

var_dump($rowset);

?>

結果:

object(Zend_Db_Table_Rowset)[6]
  protected '_data' => 
    array
      0 => 
        array
          'id' => string '1' (length=1)
          'email' => string '[email protected]' (length=24)
          'comment' => string 'Hello! Hope you enjoy this sample zf application!' (length=49)
          'created' => string '2011-10-14 09:09:56' (length=19)
  protected '_table' => 
    object(Guestbook)[3]
      protected '_definition' => null
      protected '_definitionConfigName' => null
      protected '_db' => 
        object(Zend_Db_Adapter_Pdo_Mysql)[1]
          protected '_pdoType' => string 'mysql' (length=5)
          protected '_numericDataTypes' => 
            array
              ...
          protected '_defaultStmtClass' => string 'Zend_Db_Statement_Pdo' (length=21)
          protected '_config' => 
            array
              ...
          protected '_fetchMode' => int 2
          protected '_profiler' => 
            object(Zend_Db_Profiler)[2]
              ...
          protected '_defaultProfilerClass' => string 'Zend_Db_Profiler' (length=16)
          protected '_connection' => 
            object(PDO)[4]
              ...
          protected '_caseFolding' => int 0
          protected '_autoQuoteIdentifiers' => boolean true
          protected '_allowSerialization' => boolean true
          protected '_autoReconnectOnUnserialize' => boolean false
      protected '_schema' => null
      protected '_name' => string 'Guestbook' (length=9)
      protected '_cols' => 
        array
          0 => string 'id' (length=2)
          1 => string 'email' (length=5)
          2 => string 'comment' (length=7)
          3 => string 'created' (length=7)
      protected '_primary' => 
        array
          1 => string 'id' (length=2)
      protected '_identity' => int 1
      protected '_sequence' => boolean true
      protected '_metadata' => 
        array
          'id' => 
            array
              ...
          'email' => 
            array
              ...
          'comment' => 
            array
              ...
          'created' => 
            array
              ...
      protected '_metadataCache' => null
      protected '_metadataCacheInClass' => boolean true
      protected '_rowClass' => string 'Zend_Db_Table_Row' (length=17)
      protected '_rowsetClass' => string 'Zend_Db_Table_Rowset' (length=20)
      protected '_referenceMap' => 
        array
          empty
      protected '_dependentTables' => 
        array
          empty
      protected '_defaultSource' => string 'defaultNone' (length=11)
      protected '_defaultValues' => 
        array
          empty
  protected '_connected' => boolean true
  protected '_tableClass' => string 'Guestbook' (length=9)
  protected '_rowClass' => string 'Zend_Db_Table_Row' (length=17)
  protected '_pointer' => int 0
  protected '_count' => int 1
  protected '_rows' => 
    array
      empty
  protected '_stored' => boolean true
  protected '_readOnly' => boolean false
object(Zend_Db_Table_Rowset)[7]
  protected '_data' => 
    array
      0 => 
        array
          'id' => string '1' (length=1)
          'email' => string '[email protected]' (length=24)
          'comment' => string 'Hello! Hope you enjoy this sample zf application!' (length=49)
          'created' => string '2011-10-14 09:09:56' (length=19)
      1 => 
        array
          'id' => string '2' (length=1)
          'email' => string '[email protected]' (length=11)
          'comment' => string 'Baz baz baz, baz baz Baz baz baz - baz baz baz.' (length=47)
          'created' => string '2011-10-14 09:09:56' (length=19)
  protected '_table' => 
    object(Guestbook)[3]
      protected '_definition' => null
      protected '_definitionConfigName' => null
      protected '_db' => 
        object(Zend_Db_Adapter_Pdo_Mysql)[1]
          protected '_pdoType' => string 'mysql' (length=5)
          protected '_numericDataTypes' => 
            array
              ...
          protected '_defaultStmtClass' => string 'Zend_Db_Statement_Pdo' (length=21)
          protected '_config' => 
            array
              ...
          protected '_fetchMode' => int 2
          protected '_profiler' => 
            object(Zend_Db_Profiler)[2]
              ...
          protected '_defaultProfilerClass' => string 'Zend_Db_Profiler' (length=16)
          protected '_connection' => 
            object(PDO)[4]
              ...
          protected '_caseFolding' => int 0
          protected '_autoQuoteIdentifiers' => boolean true
          protected '_allowSerialization' => boolean true
          protected '_autoReconnectOnUnserialize' => boolean false
      protected '_schema' => null
      protected '_name' => string 'Guestbook' (length=9)
      protected '_cols' => 
        array
          0 => string 'id' (length=2)
          1 => string 'email' (length=5)
          2 => string 'comment' (length=7)
          3 => string 'created' (length=7)
      protected '_primary' => 
        array
          1 => string 'id' (length=2)
      protected '_identity' => int 1
      protected '_sequence' => boolean true
      protected '_metadata' => 
        array
          'id' => 
            array
              ...
          'email' => 
            array
              ...
          'comment' => 
            array
              ...
          'created' => 
            array
              ...
      protected '_metadataCache' => null
      protected '_metadataCacheInClass' => boolean true
      protected '_rowClass' => string 'Zend_Db_Table_Row' (length=17)
      protected '_rowsetClass' => string 'Zend_Db_Table_Rowset' (length=20)
      protected '_referenceMap' => 
        array
          empty
      protected '_dependentTables' => 
        array
          empty
      protected '_defaultSource' => string 'defaultNone' (length=11)
      protected '_defaultValues' => 
        array
          empty
  protected '_connected' => boolean true
  protected '_tableClass' => string 'Guestbook' (length=9)
  protected '_rowClass' => string 'Zend_Db_Table_Row' (length=17)
  protected '_pointer' => int 0
  protected '_count' => int 2
  protected '_rows' => 
    array
      empty
  protected '_stored' => boolean true
  protected '_readOnly' => boolean false

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章