Commits

Jon Langevin committed 3f83de6

Adding scopes enhancement

Comments (0)

Files changed (3)

framework/db/ar/CActiveFinder.php

 			}
 
 			// named scope
+            $scopes=array();
 			if(($pos=strpos($with,':'))!==false)
 			{
 				$scopes=explode(':',substr($with,$pos+1));
 			}
 			if(($scope=$model->defaultScope())!==array())
 				$relation->mergeWith($scope,true);
+            $scopes=array_merge($scopes,$relation->scopes);
 			if(!empty($scopes))
 			{
 				$scs=$model->scopes();

framework/db/ar/CActiveRecord.php

 	 * referenced in this property should be disambiguated with prefix 'relationName.'.
 	 */
 	public $order='';
+    protected $_scopes=array();
 
 	/**
 	 * Constructor.
 			$this->$name=$value;
 	}
 
+    /**
+     * Array or string accepted (string format: scope1:scope2:scope3)
+     *
+     * @param mixed $scopes
+     */
+    public function setScopes($scopes) {
+        if(!is_array($scopes))
+            $scopes = explode(':', $scopes);
+        $this->_scopes = array_merge($this->_scopes, $scopes);
+    }
+
+    public function getScopes() {
+        return $this->_scopes;
+    }
+
 	/**
 	 * Merges this relation with a criteria specified dynamically.
 	 * @param array $criteria the dynamically specified criteria
 			else if($criteria['having']!=='')
 				$this->having="({$this->having}) AND ({$criteria['having']})";
 		}
+
+		if(isset($criteria['scopes']))
+			$this->setScopes($criteria['scopes']);
 	}
 }
 

framework/db/schema/CDbCriteria.php

 	 * @since 1.1.5
 	 */
 	public $index;
+    /**
+     * @var array Scopes to apply
+     */
+    protected $_scopes = array();
 
 	/**
 	 * Constructor.
 			$this->$name=$value;
 	}
 
+    public function getScopes() {
+        return $this->_scopes;
+    }
+
+    /**
+     * Array or string accepted (string format: scope1:scope2:scope3)
+     *
+     * @param mixed $scopes
+     */
+    public function setScopes($scopes) {
+        if(!is_array($scopes))
+            $scopes = explode(':', $scopes);
+        $this->_scopes = array_merge($this->_scopes, $scopes);
+    }
+
 	/**
 	 * Appends a condition to the existing {@link condition}.
 	 * The new condition and the existing condition will be concatenated via the specified operator
 			$this->with=$criteria->with;
 		else if(!empty($criteria->with))
 			$this->with=CMap::mergeArray($this->with, $criteria->with);
+
+        if($criteria->scopes!==null)
+            $this->scopes=$criteria->scopes;
 	}
 
 	/**