注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

老狗的博客

尽管每一步都很微小,但我确认我在进步

 
 
 

日志

 
 
关于我
sky

认真生活,努力工作 热爱技术,关注DB,存储,分布式,中间层,java,c++,php

网易考拉推荐

CDbCriteria 类  

2013-07-04 15:35:57|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Criteria, 英文的意思是标准,准则,规范,准绳

在这里则表明查询的条件

//添加查询条件,addCondition很粗糙,只是简单的将两个条件字符串通过操作符连接起来

//如果是column in (array(a,b,c))这种查询条件,还需要自己手工去拼凑,还需要写一些代去处理 空数组等情况

public function addCondition($condition,$operator='AND')
{
if(is_array($condition))
{
if($condition===array())
return $this;
$condition='('.implode(') '.$operator.' (',$condition).')';
}
if($this->condition==='')
$this->condition=$condition;
else
$this->condition='('.$this->condition.') '.$operator.' ('.$condition.')';
return $this;
}

//所以后续又出现了addInCondition用来处理in条件

public function addInCondition($column,$values,$operator='AND')
{
if(($n=count($values))<1)
$condition='0=1'; // 0=1 is used because in MSSQL value alone can't be used in WHERE
elseif($n===1)
{
$value=reset($values);
if($value===null)
$condition=$column.' IS NULL';
else
{
$condition=$column.'='.self::PARAM_PREFIX.self::$paramCount;
$this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;
}
}
else
{
$params=array();
foreach($values as $value)
{
$params[]=self::PARAM_PREFIX.self::$paramCount;
$this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;
}
$condition=$column.' IN ('.implode(', ',$params).')';
}
return $this->addCondition($condition,$operator);
}

//处理between条件
public function addBetweenCondition($column,$valueStart,$valueEnd,$operator='AND')
{
if($valueStart==='' || $valueEnd==='')
return $this;

$paramStart=self::PARAM_PREFIX.self::$paramCount++;
$paramEnd=self::PARAM_PREFIX.self::$paramCount++;
$this->params[$paramStart]=$valueStart;
$this->params[$paramEnd]=$valueEnd;
$condition="$column BETWEEN $paramStart AND $paramEnd";

return $this->addCondition($condition,$operator);
}

//与另外一个criteria进行合并

public function mergeWith($criteria,$operator='AND')

//compare其实非常有意思

//支持>,<,=,>=,<=,<>,like,in 操作

//如果$value是个数组,则认为是in条件

//根据$value的操作符进行判断,如果$partialMatch为true,如果操作符为'',则为like,否则为not like

//根据没有操作符,则为=

//如果有操作符,则为操作符

//这个非常强大,几乎涵盖了所有可能的条件

public function compare($column, $value, $partialMatch=false, $operator='AND', $escape=true)
{
if(is_array($value))
{
if($value===array())
return $this;
return $this->addInCondition($column,$value,$operator);
}
else
$value="$value";

if(preg_match('/^(?:\s*(<>|<=|>=|<|>|=))?(.*)$/',$value,$matches))
{
$value=$matches[2];
$op=$matches[1];
}
else
$op='';

if($value==='')
return $this;

if($partialMatch)
{
if($op==='')
return $this->addSearchCondition($column,$value,$escape,$operator);
if($op==='<>')
return $this->addSearchCondition($column,$value,$escape,$operator,'NOT LIKE');
}
elseif($op==='')
$op='=';

$this->addCondition($column.$op.self::PARAM_PREFIX.self::$paramCount,$operator);
$this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;

return $this;
}

//要选取的字段

public $select='*';
/**
* @var boolean whether to select distinct rows of data only. If this is set true,
* the SELECT clause would be changed to SELECT DISTINCT.

* 是否要唯一,使用distinct
*/
public $distinct=false;
/**
* @var string query condition. This refers to the WHERE clause in an SQL statement.
* For example, <code>age>31 AND team=1</code>.
*/
public $condition='';
/**
* @var array list of query parameter values indexed by parameter placeholders.
* For example, <code>array(':name'=>'Dan', ':age'=>31)</code>.
* 查询参数值数组,通过参数的占位符进行索引
*/
public $params=array();
/**
* @var integer maximum number of records to be returned. If less than 0, it means no limit.

/* 限制选取的行数
*/
public $limit=-1;
/**
* @var integer zero-based offset from where the records are to be returned. If less than 0, it means starting from the beginning.

* 限制偏移量
*/
public $offset=-1;
/**
* @var string how to sort the query results. This refers to the ORDER BY clause in an SQL statement.

* 进行sort的列以及顺序
*/
public $order='';
/**
* @var string how to group the query results. This refers to the GROUP BY clause in an SQL statement.
* For example, <code>'projectID, teamID'</code>.
*/
public $group='';
/**
* @var string how to join with other tables. This refers to the JOIN clause in an SQL statement.
* For example, <code>'LEFT JOIN users ON users.id=authorID'</code>.
*/
public $join='';
/**
* @var string the condition to be applied with GROUP-BY clause.
* For example, <code>'SUM(revenue)<50000'</code>.
*/
public $having='';
/**
* @var mixed the relational query criteria. This is used for fetching related objects in eager loading fashion.
* This property is effective only when the criteria is passed as a parameter to the following methods of CActiveRecord:
* <ul>
* <li>{@link CActiveRecord::find()}</li>
* <li>{@link CActiveRecord::findAll()}</li>
* <li>{@link CActiveRecord::findByPk()}</li>
* <li>{@link CActiveRecord::findAllByPk()}</li>
* <li>{@link CActiveRecord::findByAttributes()}</li>
* <li>{@link CActiveRecord::findAllByAttributes()}</li>
* <li>{@link CActiveRecord::count()}</li>
* </ul>
* The property value will be used as the parameter to the {@link CActiveRecord::with()} method
* to perform the eager loading. Please refer to {@link CActiveRecord::with()} on how to specify this parameter.
* @since 1.1.0
*/
public $with;
/**
* @var string the alias name of the table. If not set, it means the alias is 't'.
*/
public $alias;
/**
* @var boolean whether the foreign tables should be joined with the primary table in a single SQL.
* This property is only used in relational AR queries for HAS_MANY and MANY_MANY relations.
*
* When this property is set true, only a single SQL will be executed for a relational AR query,
* even if the primary table is limited and the relationship between a foreign table and the primary
* table is many-to-one.

*当together属性为true的时候,对于relational ar query将用一个sql语句来实现
*
* When this property is set false, a SQL statement will be executed for each HAS_MANY relation.
*当这个属性为false的时候,对于每一个has_mangy关系使用一个sql语句
* When this property is not set, if the primary table is limited or paginated,
* a SQL statement will be executed for each HAS_MANY relation.
* Otherwise, a single SQL statement will be executed for all.
*
* @since 1.1.4
*/
public $together;
/**
* @var string the name of the AR attribute whose value should be used as index of the query result array.
* Defaults to null, meaning the result array will be zero-based integers.
* @since 1.1.5
*/
public $index;





  评论这张
 
阅读(234)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018