2012-07-04 52 views
1

我想弄清楚Zend適配器的要領。教程和解釋很混亂,需要更多的澄清。有人能給我簡單的例子來完全理解如何使用SQL查詢以及如何獲得所需的SQL結果?如何使用Zend適配器從數據庫檢索信息

特別是,我想學習如何讓

- 列名

- 表名

- 使用fetchall項

謝謝你,

回答

4

像很多其他PEOP的你似乎對快速啓動有困難,請嘗試從Rob Allen的教程,它幫助我開始。

您對如何連接到你的表有多種選擇,與Zend_Db的混亂往往從這裏開始。

使用一個數據庫,當你application.ini文件在minmum添加這些行的最簡單方法:

//using a normal constructor 
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'  => '127.0.0.1', 
    'username' => 'webuser', 
    'password' => 'xxxxxxxx', 
    'dbname' => 'test' 
)); 

//using factory 
$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'  => '127.0.0.1', 
    'username' => 'webuser', 
    'password' => 'xxxxxxxx', 
    'dbname' => 'test' 
)); 

resources.db.adapter = "pdo_Mysql" 
resources.db.params.username = "user_name" 
resources.db.params.password = "password" 
resources.db.params.dbname = "db_name" 

另外,您可以使用Zend_Db_Adapter連接到數據庫在你的代碼幾乎任何地方

List of supported Databases

使用這個在您的應用程序可以是簡單的:

//fetchAll using Zend_Db_Adapter and plain SQL 
$sql = 'SELECT * FROM bugs WHERE bug_id = ?'; 

$result = $db->fetchAll($sql, 2); 

你可以列出數據庫中的表:

$tables = $db->listTables(); 

,或者你可以得到一個完整的表格說明(包括列名),我包括在一個Zend_Db_Adapter_Abstract功能註釋塊:

/** 
    * Returns the column descriptions for a table. 
    * 
    * The return value is an associative array keyed by the column name, 
    * as returned by the RDBMS. 
    * 
    * The value of each array element is an associative array 
    * with the following keys: 
    * 
    * SCHEMA_NAME => string; name of database or schema 
    * TABLE_NAME => string; 
    * COLUMN_NAME => string; column name 
    * COLUMN_POSITION => number; ordinal position of column in table 
    * DATA_TYPE => string; SQL datatype name of column 
    * DEFAULT  => string; default expression of column, null if none 
    * NULLABLE => boolean; true if column can have nulls 
    * LENGTH  => number; length of CHAR/VARCHAR 
    * SCALE  => number; scale of NUMERIC/DECIMAL 
    * PRECISION => number; precision of NUMERIC/DECIMAL 
    * UNSIGNED => boolean; unsigned property of an integer type 
    * PRIMARY  => boolean; true if column is part of the primary key 
    * PRIMARY_POSITION => integer; position of column in primary key 
    * 
    * @param string $tableName 
    * @param string $schemaName OPTIONAL 
    * @return array 
    */ 
$describTable = $db->describeTable('myTable'); 

這個信息應該讓你開始,但是我發現很多Zend_Db的真正力量在於在Zend_Db_TableZend_Db_Table_Row尤其是Zend_Db_Select類。

我勸你需要一些時間和數字出來。

正如你可能從Zend_Db_Table類和Zend_Db_Select對象所期望的一個例子(當不使用更先進的映射器和域對象,希望這些以後還會來):

//When using DbTable models that extend Zend_Db_Table_Abstract the model already 
//knows the name of the table and has full access to the Db adapter, allowing your code to 
//be very brief and descriptive. 
class Application_Model_DbTable_Weekend extends Zend_Db_Table_Abstract 
{ 
    //name of table, required if classname is not the same as the table name 
    protected $_name = 'weekend'; 
    //primary key column of table, a good idea especially if primary key is not 'id' 
    protected $_primary = 'weekendid'; 

    public function getWeekend($weekendId) { 
     //create select object 
     $select = $this->select(); 
     $select->where('weekendid = ?', $weekendId);//placeholder syntax 

     $result = $this->fetchRow($select);  
     if (!$result) { 
      throw new Exception('Could not find weekend ID ' . $weekendId); 
     } 
     return $result;//returns a single row object 
    } 
    public function fetchAllWeekend() { 

     $select = $this->select(); 

     $result = $this->fetchAll($select); 

     return $result; //returns array of row objects (rowset object) 
    } 
} 

羅布·艾倫的Zf的教程將介紹如何DBTABLE模型設置和他們如何工作。

希望這有助於...

相關問題