包 | system.db.ar |
---|---|
继承 | class CJoinQuery |
源自 | 1.0 |
版本 | $Id: CActiveFinder.php 3562 2012-02-13 01:27:06Z qiang.xue $ |
源码 |
CJoinQuery代表着一个关联查询 SQL 语句。
公共属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
conditions | array | WHERE 子句列表 | CJoinQuery |
distinct | boolean | 是否要选择不同的结果集 | CJoinQuery |
elements | array | 连接元素的ID列表(id=>true) | CJoinQuery |
groups | array | GROUP BY 子句列表 | CJoinQuery |
havings | array | HAVING子句列表 | CJoinQuery |
joins | array | 关联语句列表 | CJoinQuery |
limit | integer | 行limit | CJoinQuery |
offset | integer | 行偏移 | CJoinQuery |
orders | array | ORDER BY 子句列表 | CJoinQuery |
params | array | 查询参数的列表 | CJoinQuery |
selects | array | 字段选择列表 | CJoinQuery |
公共方法
方法 | 描述 | 定义在 |
---|---|---|
__construct() | 构造函数。 | CJoinQuery |
createCommand() | 创建一条SQL语句。 | CJoinQuery |
join() | 连接到另外一个关联查询节点元素。 | CJoinQuery |
属性详细
conditions
属性
public array $conditions;
WHERE 子句列表
distinct
属性
public boolean $distinct;
是否要选择不同的结果集
elements
属性
public array $elements;
连接元素的ID列表(id=>true)
groups
属性
public array $groups;
GROUP BY 子句列表
havings
属性
public array $havings;
HAVING子句列表
joins
属性
public array $joins;
关联语句列表
limit
属性
public integer $limit;
行limit
offset
属性
public integer $offset;
行偏移
orders
属性
public array $orders;
ORDER BY 子句列表
params
属性
public array $params;
查询参数的列表
selects
属性
public array $selects;
字段选择列表
方法详细
__construct()
方法
public void __construct(CJoinElement $joinElement, CDbCriteria $criteria=NULL)
| ||
$joinElement | CJoinElement | 查询树的根节点元素。 |
$criteria | CDbCriteria | 查询条件 |
public function __construct($joinElement,$criteria=null)
{
if($criteria!==null)
{
$this->selects[]=$joinElement->getColumnSelect($criteria->select);
$this->joins[]=$joinElement->getTableNameWithAlias();
$this->joins[]=$criteria->join;
$this->conditions[]=$criteria->condition;
$this->orders[]=$criteria->order;
$this->groups[]=$criteria->group;
$this->havings[]=$criteria->having;
$this->limit=$criteria->limit;
$this->offset=$criteria->offset;
$this->params=$criteria->params;
if(!$this->distinct && $criteria->distinct)
$this->distinct=true;
}
else
{
$this->selects[]=$joinElement->getPrimaryKeySelect();
$this->joins[]=$joinElement->getTableNameWithAlias();
$this->conditions[]=$joinElement->getPrimaryKeyRange();
}
$this->elements[$joinElement->id]=true;
}
构造函数。
createCommand()
方法
public string createCommand(CDbCommandBuilder $builder)
| ||
$builder | CDbCommandBuilder | 命令生成器 |
{return} | string | SQL语句 |
public function createCommand($builder)
{
$sql=($this->distinct ? 'SELECT DISTINCT ':'SELECT ') . implode(', ',$this->selects);
$sql.=' FROM ' . implode(' ',$this->joins);
$conditions=array();
foreach($this->conditions as $condition)
if($condition!=='')
$conditions[]=$condition;
if($conditions!==array())
$sql.=' WHERE (' . implode(') AND (',$conditions).')';
$groups=array();
foreach($this->groups as $group)
if($group!=='')
$groups[]=$group;
if($groups!==array())
$sql.=' GROUP BY ' . implode(', ',$groups);
$havings=array();
foreach($this->havings as $having)
if($having!=='')
$havings[]=$having;
if($havings!==array())
$sql.=' HAVING (' . implode(') AND (',$havings).')';
$orders=array();
foreach($this->orders as $order)
if($order!=='')
$orders[]=$order;
if($orders!==array())
$sql.=' ORDER BY ' . implode(', ',$orders);
$sql=$builder->applyLimit($sql,$this->limit,$this->offset);
$command=$builder->getDbConnection()->createCommand($sql);
$builder->bindValues($command,$this->params);
return $command;
}
创建一条SQL语句。
join()
方法
public void join(CJoinElement $element)
| ||
$element | CJoinElement | 等待联接的元素 |
public function join($element)
{
if($element->slave!==null)
$this->join($element->slave);
if(!empty($element->relation->select))
$this->selects[]=$element->getColumnSelect($element->relation->select);
$this->conditions[]=$element->relation->condition;
$this->orders[]=$element->relation->order;
$this->joins[]=$element->getJoinCondition();
$this->joins[]=$element->relation->join;
$this->groups[]=$element->relation->group;
$this->havings[]=$element->relation->having;
if(is_array($element->relation->params))
{
if(is_array($this->params))
$this->params=array_merge($this->params,$element->relation->params);
else
$this->params=$element->relation->params;
}
$this->elements[$element->id]=true;
}
连接到另外一个关联查询节点元素。