more query methods and fixes

This commit is contained in:
Carsten Brandt
2013-11-23 07:41:12 +01:00
parent 779d6b6e96
commit 426223af1d
5 changed files with 64 additions and 39 deletions

View File

@ -132,4 +132,24 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
return $model;
}
/**
* Returns the query result as a scalar value.
* The value returned will be the specified attribute in the first record of the query results.
* @param string $attribute name of the attribute to select
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @return string the value of the specified attribute in the first record of the query result.
* Null is returned if the query result is empty.
*/
public function scalar($attribute, $db = null)
{
$record = $this->one($db);
if ($record !== null) {
return $record->$attribute;
} else {
return null;
}
}
}

View File

@ -213,6 +213,9 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
*/
public static function updateAll($attributes, $condition = [], $params = [])
{
if (empty($condition)) {
return 0;
}
$bulk = '';
foreach((array) $condition as $pk) {
$action = Json::encode([
@ -258,8 +261,11 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
* @param array $params this parameter is ignored in redis implementation.
* @return integer the number of rows deleted
*/
public static function deleteAll($condition = null, $params = [])
public static function deleteAll($condition = [], $params = [])
{
if (empty($condition)) {
return 0;
}
$bulk = '';
foreach((array) $condition as $pk) {
$bulk = Json::encode([

View File

@ -274,7 +274,7 @@ class Command extends Component
/**
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-flush.html
*/
public function flushIndex($index)
public function flushIndex($index = '_all')
{
$response = $this->db->http()->post($this->createUrl([$index, '_flush']))->send();
return $response->getStatusCode() == 200;

View File

@ -87,20 +87,20 @@ class Query extends Component implements QueryInterface
/**
* Returns the query result as a scalar value.
* The value returned will be the first column in the first row of the query results.
* @param string $column name of the column to select
* The value returned will be the specified attribute in the first record of the query results.
* @param string $attribute name of the attribute to select
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @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.
* @return string the value of the specified attribute in the first record of the query result.
* Null is returned if the query result is empty.
*/
public function scalar($column, $db = null)
public function scalar($attribute, $db = null)
{
$record = $this->one($db);
if ($record === null) {
return false;
if ($record !== null) {
return $record->$attribute;
} else {
return $record->$column;
return null;
}
}