CActiveRelation

CDbHttpSession

Yii Framework v1.1.10 类参考

CActiveRelation

system.db.ar
继承 class CActiveRelation » CBaseActiveRelation » CComponent
子类 CBelongsToRelation, CHasManyRelation, CHasOneRelation
源自 1.0
版本 $Id: CActiveRecord.php 3533 2012-01-08 22:07:55Z mdomba $
源码 framework/db/ar/CActiveRecord.php
CActiveRelation是基类,它表示带回相关对象的活动关系。

公共属性

隐藏继承属性

属性类型描述定义在
alias string 这个关系所指的表的别名。默认为null, 意味着此别名将和此关系名一样。 CActiveRelation
className string 相关的活动记录类的名字 CBaseActiveRelation
condition string WHERE 子句。如CActiveRelation这个继承类, 关联到条件里面的字段名字应该以前缀 'relationName' 来防止歧义。 CBaseActiveRelation
foreignKey mixed 关系中的外键 CBaseActiveRelation
group string GROUP BY 子句。如CActiveRelation这个继承类, 关联到条件里面的字段名字应该以前缀 'relationName' 来防止歧义. CBaseActiveRelation
having string HAVING 子句。为CActiveRelation子类, 在此属性中引用的列名称应该以前缀 'relationName' 来防止歧义。 CBaseActiveRelation
join string 怎样跟其它数据表关联。这个跟在 SQL 语句中 JOIN 条款相同。 比如,'LEFT JOIN users ON users. CBaseActiveRelation
joinType string 连接类型。默认值为‘LEFT OUTER JOIN’。 CActiveRelation
name string 相关对象的名字 CBaseActiveRelation
on string ON 子句. CActiveRelation
order string ORDER BY 子句。为CActiveRelation子类, 在此属性中引用的列名称应该以前缀 'relationName' 来防止歧义。 CBaseActiveRelation
params array 条件中的约束数据。 键名是参数的占位符名字,值是参数值。 CBaseActiveRelation
scopes mixed 要应用的范围 可以设置为下列值之一:
  • Single scope: 'scopes'=>'scopeName'.
CActiveRelation
select mixed 要查询的字段名字。(数组或者是名字以逗号分隔的字符串)。 不要转义或者是添加前缀到这个字段名字,除非它们需要在表达式里面用到。 这种情况下,你应该以‘relationName’作为字段前缀。 CBaseActiveRelation
together boolean 是否应将此表与主表联接。 当这个属性设置为false时,和这个关系关联的表将出现在 一个单独的JOIN语句中。 如果这个属性被设置为true,相应的表总是主表连接在一起, 不管是主表被限制或没有。 如果这个属性没有设置,相应的表仅和主表连接 仅当主表不被限制时。 CActiveRelation
with string|array 指定相关的对象应该被急切的加载当这个关联对象被懒加载时。 更多关于这个属性的信息,参见CActiveRecord::with() CActiveRelation

公共方法

隐藏继承方法

方法描述定义在
__call() 如果类中没有调的方法名,则调用这个方法。 CComponent
__construct() 构造函数。 CBaseActiveRelation
__get() 返回一个属性值、一个事件处理程序列表或一个行为名称。 CComponent
__isset() 检查一个属性是否为null。 CComponent
__set() 设置一个组件的属性值。 CComponent
__unset() 设置一个组件的属性为null。 CComponent
asa() 返回这个名字的行为对象。 CComponent
attachBehavior() 附加一个行为到组件。 CComponent
attachBehaviors() 附加一个行为列表到组件。 CComponent
attachEventHandler() 为事件附加一个事件处理程序。 CComponent
canGetProperty() 确定属性是否可读。 CComponent
canSetProperty() 确定属性是否可写。 CComponent
detachBehavior() 从组件中分离一个行为。 CComponent
detachBehaviors() 从组件中分离所有行为。 CComponent
detachEventHandler() 分离一个存在的事件处理程序。 CComponent
disableBehavior() 禁用一个附加行为。 CComponent
disableBehaviors() 禁用组件附加的所有行为。 CComponent
enableBehavior() 启用一个附加行为。 CComponent
enableBehaviors() 启用组件附加的所有行为。 CComponent
evaluateExpression() 计算一个PHP表达式,或根据组件上下文执行回调。 CComponent
getEventHandlers() 返回一个事件的附加处理程序列表。 CComponent
hasEvent() 确定一个事件是否定义。 CComponent
hasEventHandler() 检查事件是否有附加的处理程序。 CComponent
hasProperty() 确定属性是否被定义。 CComponent
mergeWith() 将一个动态指定的条件合并到这个关系。 CActiveRelation
raiseEvent() 发起一个事件。 CComponent

属性详细

alias 属性
public string $alias;

这个关系所指的表的别名。默认为null, 意味着此别名将和此关系名一样。

joinType 属性
public string $joinType;

连接类型。默认值为‘LEFT OUTER JOIN’。

on 属性
public string $on;

ON 子句. 这个指定的条件将被使用AND操作符附加到连接条件。

scopes 属性 (可用自 v1.1.9)
public mixed $scopes;

要应用的范围 可以设置为下列值之一:

  • Single scope: 'scopes'=>'scopeName'.
  • Multiple scopes: 'scopes'=>array('scopeName1','scopeName2').

together 属性
public boolean $together;

是否应将此表与主表联接。 当这个属性设置为false时,和这个关系关联的表将出现在 一个单独的JOIN语句中。 如果这个属性被设置为true,相应的表总是主表连接在一起, 不管是主表被限制或没有。 如果这个属性没有设置,相应的表仅和主表连接 仅当主表不被限制时。

with 属性
public string|array $with;

指定相关的对象应该被急切的加载当这个关联对象被懒加载时。 更多关于这个属性的信息,参见CActiveRecord::with()

方法详细

mergeWith() 方法
public void mergeWith(array $criteria, boolean $fromScope=false)
$criteria array 动态指定的条件
$fromScope boolean 是否这个条件能从范围合并。
源码: framework/db/ar/CActiveRecord.php#2091 (显示)
public function mergeWith($criteria,$fromScope=false)
{
    if(
$criteria instanceof CDbCriteria)
        
$criteria=$criteria->toArray();
    if(
$fromScope)
    {
        if(isset(
$criteria['condition']) && $this->on!==$criteria['condition'])
        {
            if(
$this->on==='')
                
$this->on=$criteria['condition'];
            else if(
$criteria['condition']!=='')
                
$this->on="({$this->on}) AND ({$criteria['condition']})";
        }
        unset(
$criteria['condition']);
    }

    
parent::mergeWith($criteria);

    if(isset(
$criteria['joinType']))
        
$this->joinType=$criteria['joinType'];

    if(isset(
$criteria['on']) && $this->on!==$criteria['on'])
    {
        if(
$this->on==='')
            
$this->on=$criteria['on'];
        else if(
$criteria['on']!=='')
            
$this->on="({$this->on}) AND ({$criteria['on']})";
    }

    if(isset(
$criteria['with']))
        
$this->with=$criteria['with'];

    if(isset(
$criteria['alias']))
        
$this->alias=$criteria['alias'];

    if(isset(
$criteria['together']))
        
$this->together=$criteria['together'];
}

将一个动态指定的条件合并到这个关系。

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.