Zend_Db_Selec提供了構造SELECT查詢語句的方法。通過Zend_Db_Select構造生成的查詢語句是跨數據庫的。下面介紹Zend_Db_Select提供的常見方法。
class Zend_Db_Select的實現。可以參考類class Zend_Db_Select
<?php class Zend_Db_Select{ public function __construct(Zend_Db_Adapter_Abstract $adapter) public function distinct($flag = true) public function from($name, $cols = '*', $schema = null) public function columns($cols = '*', $correlationName = null) public function union($select = array(), $type = self::SQL_UNION) public function join($name, $cond, $cols = self::SQL_WILDCARD, $schema = null) public function joinInner($name, $cond, $cols = self::SQL_WILDCARD, $schema = null) public function joinLeft($name, $cond, $cols = self::SQL_WILDCARD, $schema = null) public function joinRight($name, $cond, $cols = self::SQL_WILDCARD, $schema = null) public function joinFull($name, $cond, $cols = self::SQL_WILDCARD, $schema = null) public function joinCross($name, $cols = self::SQL_WILDCARD, $schema = null) public function joinNatural($name, $cols = self::SQL_WILDCARD, $schema = null) public function where($cond, $value = null, $type = null) public function orWhere($cond, $value = null, $type = null) public function group($spec) public function having($cond, $value = null, $type = null) public function orHaving($cond, $value = null, $type = null) public function order($spec) public function limit($count = null, $offset = null) public function limitPage($page, $rowCount) public function forUpdate($flag = true) public function getPart($part) public function query($fetchMode = null, $bind = array()) public function assemble() public function reset($part = null) public function getAdapter() protected function _join($type, $name, $cond, $cols, $schema = null) public function _joinUsing($type, $name, $cond, $cols = '*', $schema = null) protected function _tableCols($correlationName, $cols, $afterCorrelationName = null) protected function _where($condition, $value = null, $type = null, $bool = true) protected function _getDummyTable() protected function _getQuotedSchema($schema = null) protected function _getQuotedTable($tableName, $correlationName = null) protected function _renderDistinct($sql) protected function _renderColumns($sql) protected function _renderFrom($sql) protected function _renderUnion($sql) protected function _renderWhere($sql) protected function _renderGroup($sql) protected function _renderHaving($sql) protected function _renderOrder($sql) protected function _renderLimitoffset($sql) protected function _renderForupdate($sql) public function __call($method, array $args) public function __toString() }
1.通過zend_db_table提供的select方法
$table = new Application_Model_UserTable();$db = $table->getAdapter();$db->select();
require_once 'Zend/Db.php';$params = array ( 'host' => '127.0.0.1', 'username' => 'malory', 'password' => '******', 'dbname' => 'camelot');$db = Zend_Db::factory('PDO_MYSQL', $params);$select = $db->select();// $select現在是一個Zend_Db_Select_PdoMysql對象
Zend_Db_Select(Zend_Db_Adapter_Abstract $adapter)
通過參考zend_db_select,可以很容易的學習如何使用。這里羅列手冊給出的實例。內容如下:
<?php//// SELECT *// FROM round_table// WHERE noble_title = "Sir"// ORDER BY first_name// LIMIT 10 OFFSET 20//// 你可以使用一種重復定義的方式...$select->from('round_table', '*');$select->where('noble_title = ?', 'Sir');$select->order('first_name');$select->limit(10,20);// ...或者使用一種連續定義的方式:$select->from('round_table', '*') ->where('noble_title = ?', 'Sir') ->order('first_name') ->limit(10,20);// 但是,讀取數據的方法相同$sql = $select->__toString();$result = $db->fetchAll($sql);// 對于以上任一種方式,你都可以傳送$select對象本身// 使用Zend_Db_Select對象的 __toString()方法就可以得到查詢語句$result = $db->fetchAll($select);?>
你也可以在你的查詢語句中使用綁定的參數,而不需要自己為參數加引號。
<?php//// SELECT *// FROM round_table// WHERE noble_title = "Sir"// ORDER BY first_name// LIMIT 10 OFFSET 20//$select->from('round_table', '*') ->where('noble_title = :title') ->order('first_name') ->limit(10,20);// 讀取結果使用綁定的參數$params = array('title' => 'Sir');$result = $db->fetchAll($select, $params);?>
當需要從某一個指定的表查詢某幾列時,可以使用from()方法,將需要 查詢的表名和列名都在該方法中指定。表名和列名都可以通過別名代替 ,而且也可以根據需要多次使用from()方法。
<?php// 創建一個$db對象,假定adapter為Mysql$select = $db->select();// 從some_table表中讀取a,b,c三列$select->from('some_table', 'a, b, c');// 同樣可以:$select->from('some_table', array('a', 'b', 'c');// 從foo AS bar表中讀取列bar.col$select->from('foo AS bar', 'bar.col');// 從foo, bar兩個表中讀取foo.col 別名為col1,bar.col別名為col2$select->from('foo', 'foo.col AS col1');$select->from('bar', 'bar.col AS col2');?>
當需要進行表聯合查詢時,可以使用join()方法。首先,設定進行表 聯合查詢的表名,然后是表聯合的條件(ares注:該條件是針對多表 內部連接的條件),最后是查詢的列名。同樣,你可以根據需要多次 使用join()方法。
<?php// 創建一個$db對象,假定adapter為Mysql.$select = $db->select();//// SELECT foo.*, bar.*// FROM foo// JOIN bar ON foo.id = bar.id//$select->from('foo', '*');$select->join('bar', 'foo.id = bar.id', '*');?>
目前為止,zend framework只支持普通的內部表結合語法,而不支持左結合 ,右結合,等等外部連接方式。未來版本,將會更多的支持更多的連接方式。
當需要要增加where條件時,可以使用where()方法。你可以傳送一個 普通的查詢語句字符串,也可以傳送一個使用?作為占位符的字符串,然 后在占位符處加入通過加引號處理后的數據(將使用Zend_Db_Adapter::quoteInto方法進行數據處理)
Multiple calls to where() will AND the conditions together; if you need to OR a condition, use orWhere().
<?php// 創建一個$db對象,調用SELECT方法.$select = $db->select();//// SELECT *// FROM round_table// WHERE noble_title = "Sir"// AND favorite_color = "yellow"//$select->from('round_table', '*');$select->where('noble_title = "Sir"'); // embedded value$select->where('favorite_color = ?', 'yellow'); // quoted value//// SELECT *// FROM foo// WHERE bar = "baz"// OR id IN("1", "2", "3")//$select->from('foo', '*');$select->where('bar = ?', 'baz');$select->orWhere('id IN(?)', array(1, 2, 3);?>
根據需要,可以多次使用group()方法給查詢到的數據進行分組
<?php// 創建一個$db對象,調用SELECT方法.$select = $db->select();//// SELECT COUNT(id)// FROM foo// GROUP BY bar, baz//$select->from('foo', 'COUNT(id)');$select->group('bar');$select->group('baz');// 同樣可以這樣調用 group() 方法:$select->group('bar, baz');// 還可以:$select->group(array('bar', 'baz'));?>
當需要在查詢結果中加入having條件時,可以使用having()方法。 這種方法與where()方法的功能一樣。.
當你多次調用having()方法時,各個having的條件會“并”在一起進行操作; 假如你需要實現“或 ”操作,可以使用orHaving()方法
<?php// 創建一個$db對象,調用SELECT方法.$select = $db->select();//// SELECT COUNT(id) AS count_id// FROM foo// GROUP BY bar, baz// HAVING count_id > "1"//$select->from('foo', 'COUNT(id) AS count_id');$select->group('bar, baz');$select->having('count_id > ?', 1);?>
根據需要,可以多次使用order()方法給查詢到的數據進行排序
<?php// 創建一個$db對象,調用SELECT方法.$select = $db->select();//// SELECT * FROM round_table// ORDER BY noble_title DESC, first_name ASC//$select->from('round_table', '*');$select->order('noble_title DESC');$select->order('first_name');// 同樣可以這樣調用 order() 方法:$select->order('noble_title DESC, first_name');// 還可以:$select->order(array('noble_title DESC', 'first_name'));?>
Zend_db_select可以支持數據庫層的limit語句限制。對于一些數據庫,例如mysql 和postgresql,實現這些是相對容易的,因為這些數據庫本身就支持“limit:count” 語法。
對于其他一些數據庫來說,例如微軟的sqlserver和oracle,要實現limit功能 就不那么簡單了,因為他們本身就根本不支持limit語句。MS-SQL有一個top語 句來實現,而oracle要實現limit功能,查詢語句的寫法就更特殊一些。由于 zend_db_select內在地工作的方式,我們可以重寫select語句以在oracle中 實現上述開源數據庫系統的limit功能。
要通過設定查詢的總數和偏移量對返回的結果進行限制,可以使用limit()方法, 總數值和一個可選的偏移量作為調用該方法的參數。
<?php// 首先,一個簡單的 "LIMIT :count"$select = $db->select();$select->from('foo', '*');$select->order('id');$select->limit(10);//// 在mysql/psotgreSql/SQLite,可以得到這樣的語句://// SELECT * FROM foo// ORDER BY id ASC// LIMIT 10//// 但是在Microsoft SQL下,可以得到這樣的語句://// SELECT TOP 10 * FROM FOO// ORDER BY id ASC////// 現在, 是更復雜的 "LIMIT :count OFFSET :offset"方法$select = $db->select();$select->from('foo', '*');$select->order('id');$select->limit(10, 20);//// 在mysql/psotgreSql/SQLite,可以得到這樣的語句://// SELECT * FROM foo// ORDER BY id ASC// LIMIT 10 OFFSET 20//// 但是在Microsoft SQL下,由于不支持偏移量功能,可以得到這樣sql語句://// SELECT * FROM (// SELECT TOP 10 * FROM (// SELECT TOP 30 * FROM foo ORDER BY id DESC// ) ORDER BY id ASC// )//// Zend_Db_Adapter 可以自動的完成sql語句的動態創建.//?>
Zend_db_select同樣也提供了翻頁的limit功能。假如你想要從結果中找到 特定“頁數”的結果,使用limitPage()方法;將你需要的頁數值和每頁顯示 的數據值數作為參數傳過去即可.
<?php// 構造基礎的select方法:$select = $db->select();$select->from('foo', '*');$select->order('id');// ... 限制到第三頁,每頁包括10行數據$select->limitPage(3, 10);//// 在MySQL/PostgreSQL/SQLite下, 可以得到://// SELECT * FROM foo// ORDER BY id ASC// LIMIT 10 OFFSET 20//?>