mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-10 02:13:17 +08:00
more query methods and fixes
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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([
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user