CActiveFinder
包 | system.db.ar |
---|---|
继承 | class CActiveFinder » CComponent |
可用自 | 1.0 |
源码 | 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.
When used in eager loading, this class provides the same set of find methods as CActiveRecord.
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
baseLimited | boolean | whether the base model has limit or offset. | CActiveFinder |
joinAll | boolean | join all tables all at once. | CActiveFinder |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__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 method is internally called. | 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 |
evaluateExpression() | Evaluates a PHP expression or callback under the context of this component. | CComponent |
findAllBySql() | This method is internally called. | CActiveFinder |
findBySql() | This method is internally called. | CActiveFinder |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
getModel() | Given active record class name returns new model instance. | CActiveFinder |
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 |
query() | Do not call this method. This method is used internally to perform the relational query | CActiveFinder |
raiseEvent() | Raises an event. | CComponent |
属性详情
baseLimited
属性
public boolean $baseLimited;
whether the base model has limit or offset. This property is internally used.
joinAll
属性
public boolean $joinAll;
join all tables all at once. Defaults to false. This property is internally used.
方法详情
__construct()
方法
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 |
源码: framework/db/ar/CActiveFinder.php#44 (显示)
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()
方法
public string count(CDbCriteria $criteria)
| ||
$criteria | CDbCriteria | the query criteria |
{return} | string |
源码: framework/db/ar/CActiveFinder.php#142 (显示)
public function count($criteria)
{
Yii::trace(get_class($this->_joinTree->model).'.count() eagerly','system.db.ar.CActiveRecord');
$this->joinAll=$criteria->together!==true;
$alias=$criteria->alias===null ? 't' : $criteria->alias;
$this->_joinTree->tableAlias=$alias;
$this->_joinTree->rawTableAlias=$this->_builder->getSchema()->quoteTableName($alias);
$n=$this->_joinTree->count($criteria);
$this->destroyJoinTree();
return $n;
}
This method is internally called.
findAllBySql()
方法
public CActiveRecord[] findAllBySql(string $sql, array $params=array (
))
| ||
$sql | string | the SQL statement |
$params | array | parameters to be bound to the SQL statement |
{return} | CActiveRecord[] |
源码: framework/db/ar/CActiveFinder.php#119 (显示)
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->findWithBase($baseRecords);
$this->_joinTree->afterFind();
$this->destroyJoinTree();
return $baseRecords;
}
else
{
$this->destroyJoinTree();
return array();
}
}
This method is internally called.
findBySql()
方法
public CActiveRecord findBySql(string $sql, array $params=array (
))
| ||
$sql | string | the SQL statement |
$params | array | parameters to be bound to the SQL statement |
{return} | CActiveRecord |
源码: framework/db/ar/CActiveFinder.php#98 (显示)
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->findWithBase($baseRecord);
$this->_joinTree->afterFind();
$this->destroyJoinTree();
return $baseRecord;
}
else
$this->destroyJoinTree();
}
This method is internally called.
getModel()
方法
(自版本 v1.1.14 可用)
public CActiveRecord getModel(string $className)
| ||
$className | string | active record class name |
{return} | CActiveRecord | active record model instance |
源码: framework/db/ar/CActiveFinder.php#180 (显示)
public function getModel($className)
{
return CActiveRecord::model($className);
}
Given active record class name returns new model instance.
lazyFind()
方法
public void lazyFind(CActiveRecord $baseRecord)
| ||
$baseRecord | CActiveRecord | the base record whose related objects are to be loaded |
源码: framework/db/ar/CActiveFinder.php#161 (显示)
public function lazyFind($baseRecord)
{
$this->_joinTree->lazyFind($baseRecord);
if(!empty($this->_joinTree->children))
{
foreach($this->_joinTree->children as $child)
$child->afterFind();
}
$this->destroyJoinTree();
}
Finds the related objects for the specified active record. This method is internally invoked by CActiveRecord to support lazy loading.
query()
方法
public mixed query(CDbCriteria $criteria, boolean $all=false)
| ||
$criteria | CDbCriteria | the DB criteria |
$all | boolean | whether to bring back all records |
{return} | mixed | the query result |
源码: framework/db/ar/CActiveFinder.php#58 (显示)
public function query($criteria,$all=false)
{
$this->joinAll=$criteria->together===true;
if($criteria->alias!='')
{
$this->_joinTree->tableAlias=$criteria->alias;
$this->_joinTree->rawTableAlias=$this->_builder->getSchema()->quoteTableName($criteria->alias);
}
$this->_joinTree->find($criteria);
$this->_joinTree->afterFind();
if($all)
{
$result = array_values($this->_joinTree->records);
if ($criteria->index!==null)
{
$index=$criteria->index;
$array=array();
foreach($result as $object)
$array[$object->$index]=$object;
$result=$array;
}
}
elseif(count($this->_joinTree->records))
$result = reset($this->_joinTree->records);
else
$result = null;
$this->destroyJoinTree();
return $result;
}
Do not call this method. This method is used internally to perform the relational query based on the given DB criteria.