CJoinQuery

优质
小牛编辑
131浏览
2023-12-01
所有包 | 属性 | 方法
system.db.ar
继承class CJoinQuery
源自1.0
版本$Id: CActiveFinder.php 3562 2012-02-13 01:27:06Z qiang.xue $
源码framework/db/ar/CActiveFinder.php
CJoinQuery代表着一个关联查询 SQL 语句。

公共属性

隐藏继承属性

属性类型描述定义在
conditionsarrayWHERE 子句列表CJoinQuery
distinctboolean是否要选择不同的结果集CJoinQuery
elementsarray连接元素的ID列表(id=>true)CJoinQuery
groupsarrayGROUP BY 子句列表CJoinQuery
havingsarrayHAVING子句列表CJoinQuery
joinsarray关联语句列表CJoinQuery
limitinteger行limitCJoinQuery
offsetinteger行偏移CJoinQuery
ordersarrayORDER BY 子句列表CJoinQuery
paramsarray查询参数的列表CJoinQuery
selectsarray字段选择列表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)
$joinElementCJoinElement查询树的根节点元素。
$criteriaCDbCriteria查询条件
源码: framework/db/ar/CActiveFinder.php#1265 (显示) publicfunction__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)
$builderCDbCommandBuilder命令生成器
{return}stringSQL语句
源码: framework/db/ar/CActiveFinder.php#1323 (显示) publicfunctioncreateCommand($builder)
{
$sql=($this->distinct?'SELECTDISTINCT':'SELECT').implode(',',$this->selects);
$sql.='FROM'.implode('',$this->joins);

$conditions=array();
foreach($this->conditionsas$condition)
if($condition!=='')
$conditions[]=$condition;
if($conditions!==array())
$sql.='WHERE('.implode(')AND(',$conditions).')';

$groups=array();
foreach($this->groupsas$group)
if($group!=='')
$groups[]=$group;
if($groups!==array())
$sql.='GROUPBY'.implode(',',$groups);

$havings=array();
foreach($this->havingsas$having)
if($having!=='')
$havings[]=$having;
if($havings!==array())
$sql.='HAVING('.implode(')AND(',$havings).')';

$orders=array();
foreach($this->ordersas$order)
if($order!=='')
$orders[]=$order;
if($orders!==array())
$sql.='ORDERBY'.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)
$elementCJoinElement等待联接的元素
源码: framework/db/ar/CActiveFinder.php#1295 (显示) publicfunctionjoin($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;
}

连接到另外一个关联查询节点元素。