Added count, average, sum, min, max, scalar methods to ActiveQuery.

Added support for scopes defined in AR classes.
This commit is contained in:
Qiang Xue
2013-01-17 19:26:20 -05:00
parent 63bb6efb6e
commit b51d347465
5 changed files with 110 additions and 46 deletions

View File

@@ -26,7 +26,12 @@ use yii\db\Exception;
*
* - [[one()]]: returns a single record populated with the first row of data.
* - [[all()]]: returns all records based on the query results.
* - [[value()]]: returns the value of the first column in the first row of the query result.
* - [[count()]]: returns the number of records.
* - [[sum()]]: returns the sum over the specified column.
* - [[average()]]: returns the average over the specified column.
* - [[min()]]: returns the min over the specified column.
* - [[max()]]: returns the max over the specified column.
* - [[scalar()]]: returns the value of the first column in the first row of the query result.
* - [[exists()]]: returns a value indicating whether the query result has data or not.
*
* Because ActiveQuery extends from [[Query]], one can use query methods, such as [[where()]],
@@ -74,6 +79,24 @@ class ActiveQuery extends Query
public $sql;
/**
* PHP magic method.
* This method allows calling static method defined in [[modelClass]] via this query object.
* It is mainly implemented for supporting the feature of scope.
* @param string $name the method name to be called
* @param array $params the parameters passed to the method
* @return mixed the method return result
*/
public function __call($name, $params)
{
if (method_exists($this->modelClass, $name)) {
array_unshift($params, $this);
return call_user_func_array(array($this->modelClass, $name), $params);
} else {
return parent::__call($name, $params);
}
}
/**
* Executes query and returns all results as an array.
* @return array the query results. If the query results in nothing, an empty array will be returned.
@@ -118,13 +141,73 @@ class ActiveQuery extends Query
}
}
/**
* Returns the number of records.
* @param string $q the COUNT expression. Defaults to '*'.
* Make sure you properly quote column names.
* @return integer number of records
*/
public function count($q = '*')
{
$this->select = array("COUNT($q)");
return $this->createCommand()->queryScalar();
}
/**
* Returns the sum of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly quote column names.
* @return integer the sum of the specified column values
*/
public function sum($q)
{
$this->select = array("SUM($q)");
return $this->createCommand()->queryScalar();
}
/**
* Returns the average of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly quote column names.
* @return integer the average of the specified column values.
*/
public function average($q)
{
$this->select = array("AVG($q)");
return $this->createCommand()->queryScalar();
}
/**
* Returns the minimum of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly quote column names.
* @return integer the minimum of the specified column values.
*/
public function min($q)
{
$this->select = array("MIN($q)");
return $this->createCommand()->queryScalar();
}
/**
* Returns the maximum of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly quote column names.
* @return integer the maximum of the specified column values.
*/
public function max($q)
{
$this->select = array("MAX($q)");
return $this->createCommand()->queryScalar();
}
/**
* Returns the query result as a scalar value.
* The value returned will be the first column in the first row of the query results.
* @return string|boolean the value of the first column in the first row of the query result.
* False is returned if the query result is empty.
*/
public function value()
public function scalar()
{
return $this->createCommand()->queryScalar();
}
@@ -136,7 +219,7 @@ class ActiveQuery extends Query
public function exists()
{
$this->select = array(new Expression('1'));
return $this->value() !== false;
return $this->scalar() !== false;
}
/**

View File

@@ -127,36 +127,6 @@ class ActiveRecord extends Model
return $query->params($params);
}
/**
* Creates a `COUNT` query for this AR class.
*
* Below are some usage examples:
*
* ~~~
* // count the total number of customers
* echo Customer::count()->value();
* // count the number of active customers:
* echo Customer::count()
* ->where(array('status' => 1))
* ->value();
* // customize the count expression
* echo Customer::count('COUNT(DISTINCT age)')->value();
* ~~~
*
* @param string $q the count expression. If null, it means `COUNT(*)`.
* @return ActiveQuery the newly created [[ActiveQuery]] instance
*/
public static function count($q = null)
{
$query = static::createQuery();
if ($q !== null) {
$query->select = array($q);
} elseif ($query->select === null) {
$query->select = array('COUNT(*)');
}
return $query;
}
/**
* Updates the whole table using the provided attribute values and conditions.
* For example, to change the status to be 1 for all customers whose status is 2:
@@ -646,7 +616,7 @@ class ActiveRecord extends Model
$values[$key] = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null;
}
}
$db = $this->getDbConnection();
$db = static::getDbConnection();
$command = $db->createCommand()->insert($this->tableName(), $values);
if ($command->execute()) {
$table = $this->getTableSchema();
@@ -1082,7 +1052,7 @@ class ActiveRecord extends Model
foreach ($extraColumns as $k => $v) {
$columns[$k] = $v;
}
$this->getDbConnection()->createCommand()
static::getDbConnection()->createCommand()
->insert($viaTable, $columns)->execute();
} else {
$p1 = $model->isPrimaryKey(array_keys($relation->link));
@@ -1153,7 +1123,7 @@ class ActiveRecord extends Model
foreach ($relation->link as $a => $b) {
$columns[$b] = $model->$a;
}
$command = $this->getDbConnection()->createCommand();
$command = static::getDbConnection()->createCommand();
if ($delete) {
$command->delete($viaTable, $columns)->execute();
} else {