Permalink
Browse files

(Fixes issue 730)

  • Loading branch information...
1 parent ff125a2 commit a5bc9a5249bcac6ced4bfcbbf9021c3ee0e8bf7d qiang.xue committed Dec 15, 2009
Showing with 13 additions and 20 deletions.
  1. +1 −1 CHANGELOG
  2. +3 −15 framework/db/ar/CActiveFinder.php
  3. +9 −4 framework/db/ar/CActiveRecord.php
View
@@ -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
--------------------------------
@@ -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);
@@ -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();
@@ -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);
}
@@ -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)
{
@@ -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;

0 comments on commit a5bc9a5

Please sign in to comment.