日期:2011-07-08  浏览次数:20442 次

/**** 
Query Builder  
 
translated by php攻城师 
 
http://blog.csdn.net/phpgcs 
 
Preparing Query Builder             准备 Query Builder         
Building Data Retrieval Queries     构建数据查找查询 
Building Data Manipulation Queries  构建数据操作查询 
Building Schema Manipulation Queries构建数据结构操作查询 
 
 
****/  
Yii Query Builder 提供了一个以面向对象的方式写SQL表达式的方法。  
允许开发者用 类的方法和属性来 明确一个SQL表达式中的独立的单元。然后将不同的单元组装成一个合法的SQL表达式,进而让DAO方法调用和执行。  
下面是一个典型的使用Yii Query Builder来建立一个SELECT SQL 语句的例子:  
  
$user = Yii::app()->db->createCommand()  
    ->select('id, username, profile')  
    ->from('tbl_user u')  
    ->join('tbl_profile p', 'u.id=p.user_id')  
    ->where('id=:id', array(':id'=>$id))  
    ->queryRow();  
  
当你需要程序化地组装一个SQL语句时,或者基于一些额外的逻辑在你的应用中时, 用Yii Query Builder是最好不过的了。  
  
主要的好处是:  
  
1,  允许程序化建立一个复杂的SQL表达式  
2, 自动引用表明和列名来 防止跟SQL保留关键字以及特殊字符的冲突  
3, 在可以的情况下引用参数值,使用参数绑定,从而降低了SQL 注入攻击的风险。  
4, 提供一定程度的DB抽象, 从而简化了向不同DB平台迁移的工作。  
  
并不是强制要使用 Query Builder, 事实上, 如果你的查询很简单,还是直接写SQL 语句来的快捷方便。  
  
注意: Query Builder 不可以被用语修改一个已经被定制了的SQL表达式查询。如下代码是不会工作的:  
  
$command = Yii::app()->db->createCommand('SELECT * FROM tbl_user');  
// the following line will NOT append WHERE clause to the above SQL  
$command->where('id=:id', array(':id'=>$id));  
  
换句话说, 不要把 普通SQL 和 Query Builder 混合使用!  
  
  
/***** 
 
1. Preparing Query Builder  
 
translated by php攻城师 
 
http://blog.csdn.net/phpgcs 
 
*****/  
  
Query Builder 是跟 CDbCommand 相关联的, 主 DB 查询类 定义在 DAO 中。  
要开始使用 Query Builder, 我们创建一个 CDbCommand 实例如下:  
  
$command = Yii::app()->db->createCommand();  
  
我们使用 Yii::app()->db 获得 DB connection, 然后用 CDbConnection::createCommand() 来创建实例.  
  
注意:这里我们不是想在DAO 中把一整个SQL语句给了 createCommand() , 而是留空了。  
这是因为我们将在后面使用  Query Builder 的方法来构建 这个SQL 表达式的不同部分。  
  
  
/***** 
 
2. Building Data Retrieval Queries  
 
translated by php攻城师 
 
http://blog.csdn.net/phpgcs 
 
*****/  
  
Data retrieval queries 指的是 SELECT SQL statements.   
  
query builder 提供了一系列方法来建立一个 SELCET 语句的不同部分。  
因为所有这些方法返回了 CDbCommand 实例 ,我们可以通过使用 方法链来调用它们。如下:  
  
select(): specifies the SELECT part of the query  
selectDistinct(): specifies the SELECT part of the query and turns on the DISTINCT flag  
from(): specifies the FROM part of the query  
where(): specifies the WHERE part of the query  
andWhere(): appends condition to the WHERE part of the query with AND operator  
orWhere(): appends condition to the WHERE part of the query with OR operator  
join(): appends an inner join query fragment  
leftJoin(): appends a left outer join query fragment  
rightJoin(): appends a right outer join query fragment  
crossJoin(): appends a cross join query fragment  
naturalJoin(): appends a natural join query fragment  
group(): specifies the GROUP BY part of the query  
having(): specifies the HAVING part of the query  
order(): specifies the ORDER BY part of the query  
limit(): specifies the LIMIT part of the query  
offset(): specifies the OFFSET part of the query  
union(): appends a UNION query fragment  
In the following, we explain how to use these query builder methods. For simplicity, we assume the underlying database is MySQL. Note that if you are using other DBMS, the table/column/value quoting shown in the examples may be different.  
  
select()  
function select($columns='*')  
  
这个方法定制了查询的SELECT 部分。  
参数 $columns 定制了将要被选择的列, 既可以是被逗号分隔开的列, 也可以是一个由列名构成的数组。  
列名可以包含表前缀 和(或) 列别名。  
这个方法将自动引用列名,除非某个列 包含了插入语(意味着那个列是由一个DB表达式提供)  
  
看例子:  
  
// SELECT *  
select()  
// SELECT `id`, `username`  
select('id, username')  
// SELECT `tbl_user`.`id`, `username` AS `name`  
select('tbl_user.id, username as name')  
// SELECT `id`, `username`  
select(array('id', 'username'))  
// SELECT `id`, count(*) as num  
select(array('id', 'count(*) as num'))  
  
  
from()  
function from($tables)  
  
看例子:  
//