0 follower

CActiveFinder

Package system.db.ar
Inheritance class CActiveFinder » CComponent
Since 1.0
Version $Id$
Source Code framework/db/ar/CActiveFinder.php
CActiveFinder implements eager loading and lazy loading of related active records.

When used in eager loading, this class provides the same set of find methods as CActiveRecord.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
baseLimited boolean whether the base model has limit or offset. CActiveFinder
joinAll boolean join all tables all at once. CActiveFinder

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CActiveFinder
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
count() This is the relational version of CActiveRecord::count(). CActiveFinder
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
find() This is the relational version of CActiveRecord::find(). CActiveFinder
findAll() This is the relational version of CActiveRecord::findAll(). CActiveFinder
findAllByAttributes() This is the relational version of CActiveRecord::findAllByAttributes(). CActiveFinder
findAllByPk() This is the relational version of CActiveRecord::findAllByPk(). CActiveFinder
findAllBySql() This is the relational version of CActiveRecord::findAllBySql(). CActiveFinder
findByAttributes() This is the relational version of CActiveRecord::findByAttributes(). CActiveFinder
findByPk() This is the relational version of CActiveRecord::findByPk(). CActiveFinder
findBySql() This is the relational version of CActiveRecord::findBySql(). CActiveFinder
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
lazyFind() Finds the related objects for the specified active record. CActiveFinder
raiseEvent() Raises an event. CComponent
together() Uses the most aggressive join approach. CActiveFinder

Property Details

baseLimited property (available since v1.0.2)
public boolean $baseLimited;

whether the base model has limit or offset. This property is internally used.

joinAll property (available since v1.0.2)
public boolean $joinAll;

join all tables all at once. Defaults to false. This property is internally used.

Method Details

__construct() method
public void __construct(CActiveRecord $model, mixed $with)
$model CActiveRecord the model that initiates the active finding process
$with mixed the relation names to be actively looked for
Source Code: framework/db/ar/CActiveFinder.php#47 (show)
public function __construct($model,$with)
{
    
$this->_builder=$model->getCommandBuilder();
    
$this->_joinTree=new CJoinElement($this,$model);
    
$this->buildJoinTree($this->_joinTree,$with);
}

Constructor. A join tree is built up based on the declared relationships between active record classes.

count() method (available since v1.0.3)
public void count($condition='', $params=array ( ))
$condition
$params
Source Code: framework/db/ar/CActiveFinder.php#186 (show)
public function count($condition='',$params=array())
{
    
Yii::trace(get_class($this->_joinTree->model).'.count() eagerly','system.db.ar.CActiveRecord');
    
$criteria=$this->_builder->createCriteria($condition,$params);
    
$this->_joinTree->model->applyScopes($criteria);
    return 
$this->_joinTree->count($criteria);
}

This is the relational version of CActiveRecord::count().

find() method
public void find($condition='', $params=array ( ))
$condition
$params
Source Code: framework/db/ar/CActiveFinder.php#91 (show)
public function find($condition='',$params=array())
{
    
Yii::trace(get_class($this->_joinTree->model).'.find() eagerly','system.db.ar.CActiveRecord');
    
$criteria=$this->_builder->createCriteria($condition,$params);
    return 
$this->query($criteria);
}

This is the relational version of CActiveRecord::find().

findAll() method
public void findAll($condition='', $params=array ( ))
$condition
$params
Source Code: framework/db/ar/CActiveFinder.php#101 (show)
public function findAll($condition='',$params=array())
{
    
Yii::trace(get_class($this->_joinTree->model).'.findAll() eagerly','system.db.ar.CActiveRecord');
    
$criteria=$this->_builder->createCriteria($condition,$params);
    return 
$this->query($criteria,true);
}

This is the relational version of CActiveRecord::findAll().

findAllByAttributes() method
public void findAllByAttributes($attributes, $condition='', $params=array ( ))
$attributes
$condition
$params
Source Code: framework/db/ar/CActiveFinder.php#141 (show)
public function findAllByAttributes($attributes,$condition='',$params=array())
{
    
Yii::trace(get_class($this->_joinTree->model).'.findAllByAttributes() eagerly','system.db.ar.CActiveRecord');
    
$criteria=$this->_builder->createColumnCriteria($this->_joinTree->model->getTableSchema(),$attributes,$condition,$params);
    return 
$this->query($criteria,true);
}

This is the relational version of CActiveRecord::findAllByAttributes().

findAllByPk() method
public void findAllByPk($pk, $condition='', $params=array ( ))
$pk
$condition
$params
Source Code: framework/db/ar/CActiveFinder.php#121 (show)
public function findAllByPk($pk,$condition='',$params=array())
{
    
Yii::trace(get_class($this->_joinTree->model).'.findAllByPk() eagerly','system.db.ar.CActiveRecord');
    
$criteria=$this->_builder->createPkCriteria($this->_joinTree->model->getTableSchema(),$pk,$condition,$params);
    return 
$this->query($criteria,true);
}

This is the relational version of CActiveRecord::findAllByPk().

findAllBySql() method
public void findAllBySql($sql, $params=array ( ))
$sql
$params
Source Code: framework/db/ar/CActiveFinder.php#167 (show)
public function findAllBySql($sql,$params=array())
{
    
Yii::trace(get_class($this->_joinTree->model).'.findAllBySql() eagerly','system.db.ar.CActiveRecord');
    if((
$rows=$this->_builder->createSqlCommand($sql,$params)->queryAll())!==array())
    {
        
$baseRecords=$this->_joinTree->model->populateRecords($rows,false);
        
$this->_joinTree->beforeFind();
        
$this->_joinTree->findWithBase($baseRecords);
        
$this->_joinTree->afterFind();
        return 
$baseRecords;
    }
    else
        return array();
}

This is the relational version of CActiveRecord::findAllBySql().

findByAttributes() method
public void findByAttributes($attributes, $condition='', $params=array ( ))
$attributes
$condition
$params
Source Code: framework/db/ar/CActiveFinder.php#131 (show)
public function findByAttributes($attributes,$condition='',$params=array())
{
    
Yii::trace(get_class($this->_joinTree->model).'.findByAttributes() eagerly','system.db.ar.CActiveRecord');
    
$criteria=$this->_builder->createColumnCriteria($this->_joinTree->model->getTableSchema(),$attributes,$condition,$params);
    return 
$this->query($criteria);
}

This is the relational version of CActiveRecord::findByAttributes().

findByPk() method
public void findByPk($pk, $condition='', $params=array ( ))
$pk
$condition
$params
Source Code: framework/db/ar/CActiveFinder.php#111 (show)
public function findByPk($pk,$condition='',$params=array())
{
    
Yii::trace(get_class($this->_joinTree->model).'.findByPk() eagerly','system.db.ar.CActiveRecord');
    
$criteria=$this->_builder->createPkCriteria($this->_joinTree->model->getTableSchema(),$pk,$condition,$params);
    return 
$this->query($criteria);
}

This is the relational version of CActiveRecord::findByPk().

findBySql() method
public void findBySql($sql, $params=array ( ))
$sql
$params
Source Code: framework/db/ar/CActiveFinder.php#151 (show)
public function findBySql($sql,$params=array())
{
    
Yii::trace(get_class($this->_joinTree->model).'.findBySql() eagerly','system.db.ar.CActiveRecord');
    if((
$row=$this->_builder->createSqlCommand($sql,$params)->queryRow())!==false)
    {
        
$baseRecord=$this->_joinTree->model->populateRecord($row,false);
        
$this->_joinTree->beforeFind();
        
$this->_joinTree->findWithBase($baseRecord);
        
$this->_joinTree->afterFind();
        return 
$baseRecord;
    }
}

This is the relational version of CActiveRecord::findBySql().

lazyFind() method
public void lazyFind(CActiveRecord $baseRecord)
$baseRecord CActiveRecord the base record whose related objects are to be loaded
Source Code: framework/db/ar/CActiveFinder.php#199 (show)
public function lazyFind($baseRecord)
{
    
$this->_joinTree->lazyFind($baseRecord);
    if(!empty(
$this->_joinTree->children))
    {
        
$child=reset($this->_joinTree->children);
        
$child->afterFind();
    }
}

Finds the related objects for the specified active record. This method is internally invoked by CActiveRecord to support lazy loading.

together() method (available since v1.0.2)
public CActiveFinder together(boolean $ignoreLimit=true)
$ignoreLimit boolean whether we should enforce join even when a limit option is placed on the primary table query. Defaults to true. If false, we would still use two queries when there is a HAS_MANY/MANY_MANY relation and the primary table has a LIMIT option. This parameter is available since version 1.0.3.
{return} CActiveFinder the finder object
Source Code: framework/db/ar/CActiveFinder.php#65 (show)
public function together($ignoreLimit=true)
{
    
$this->joinAll=true;
    if(
$ignoreLimit)
        
$this->baseLimited=false;
    return 
$this;
}

Uses the most aggressive join approach. By default, several join statements may be generated in order to avoid fetching duplicated data. By calling this method, all tables will be joined together all at once.