Skip to content

Commit

Permalink
(Fixes issue 730)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiang.xue committed Dec 15, 2009
1 parent ff125a2 commit a5bc9a5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
Expand Up @@ -4,7 +4,7 @@

Version 1.0.12 to be released
-----------------------------

- Enh #730: Relational queries now respect changes made to CActiveRecord::dbCriteria in the onBeforeFind event (Qiang)

Version 1.0.11 December 13, 2009
--------------------------------
Expand Down
18 changes: 3 additions & 15 deletions framework/db/ar/CActiveFinder.php
Expand Up @@ -37,18 +37,15 @@ class CActiveFinder extends CComponent
private $_joinCount=0;
private $_joinTree;
private $_builder;
private $_criteria; // the criteria generated via named scope

/**
* Constructor.
* A join tree is built up based on the declared relationships between active record classes.
* @param CActiveRecord the model that initiates the active finding process
* @param mixed the relation names to be actively looked for
* @param CDbCriteria the criteria associated with the named scopes (since version 1.0.5)
*/
public function __construct($model,$with,$criteria=null)
public function __construct($model,$with)
{
$this->_criteria=$criteria;
$this->_builder=$model->getCommandBuilder();
$this->_joinTree=new CJoinElement($this,$model);
$this->buildJoinTree($this->_joinTree,$with);
Expand All @@ -75,13 +72,8 @@ public function together($ignoreLimit=true)

private function query($criteria,$all=false)
{
if($this->_criteria!==null)
{
$this->_criteria->mergeWith($criteria);
$criteria=$this->_criteria;
}

$this->_joinTree->beforeFind();
$this->_joinTree->model->applyScopes($criteria);
$this->_joinTree->find($criteria);
$this->_joinTree->afterFind();

Expand Down Expand Up @@ -195,11 +187,7 @@ 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);
if($this->_criteria!==null)
{
$this->_criteria->mergeWith($criteria);
$criteria=$this->_criteria;
}
$this->_joinTree->model->applyScopes($criteria);
return $this->_joinTree->count($criteria);
}

Expand Down
13 changes: 9 additions & 4 deletions framework/db/ar/CActiveRecord.php
Expand Up @@ -1461,7 +1461,14 @@ private function query($criteria,$all=false)
return $all ? $this->populateRecords($command->queryAll()) : $this->populateRecord($command->queryRow());
}

private function applyScopes(&$criteria)
/**
* Applies the query scopes to the given criteria.
* This method merges {@link dbCriteria} with the given criteria parameter.
* It then resets {@link dbCriteria} to be null.
* @param CDbCriteria the query criteria. This parameter may be modified by merging {@link dbCriteria}.
* @since 1.0.12
*/
public function applyScopes(&$criteria)
{
if(($c=$this->getDbCriteria(false))!==null)
{
Expand Down Expand Up @@ -1681,9 +1688,7 @@ public function with()
$with=func_get_args();
if(is_array($with[0])) // the parameter is given as an array
$with=$with[0];
$finder=new CActiveFinder($this,$with,$this->getDbCriteria(false));
$this->_c=null;
return $finder;
return new CActiveFinder($this,$with);
}
else
return $this;
Expand Down

0 comments on commit a5bc9a5

Please sign in to comment.