Commit 8b4dfcc8 by Qiang Xue

Fixes #2955: Changed the signature of ActiveQuery constructors and…

Fixes #2955: Changed the signature of ActiveQuery constructors and `ActiveRecord::createQuery()` to simplify customizing ActiveQuery classes
parent 3ea63437
......@@ -838,10 +838,9 @@ use yii\db\ActiveRecord;
class Comment extends ActiveRecord
{
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new CommentQuery($config);
return new CommentQuery(get_called_class());
}
}
```
......@@ -910,10 +909,9 @@ If you used Yii 1.1 before, you may know a concept called *default scope*. A def
applies to ALL queries. You can define a default scope easily by overriding [[yii\db\ActiveRecord::createQuery()]]. For example,
```php
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return (new ActiveQuery($config))->where(['deleted' => false]);
return parent::createQuery()->where(['deleted' => false]);
}
```
......
......@@ -77,6 +77,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
use ActiveQueryTrait;
use ActiveRelationTrait;
/**
* Creates a DB command that can be used to execute this query.
* @param Connection $db the DB connection used to create the DB command.
......
......@@ -156,23 +156,20 @@ class ActiveRecord extends BaseActiveRecord
* You may also define default conditions that should apply to all queries unless overridden:
*
* ```php
* public static function createQuery($config = [])
* public static function createQuery()
* {
* return parent::createQuery($config)->where(['deleted' => false]);
* return parent::createQuery()->where(['deleted' => false]);
* }
* ```
*
* Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the
* default condition. Using [[Query::where()]] will override the default condition.
*
* @param array $config the configuration passed to the ActiveQuery class.
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new ActiveQuery($config);
return new ActiveQuery(get_called_class());
}
// TODO implement copy and move as pk change is not possible
......
......@@ -102,23 +102,20 @@ abstract class ActiveRecord extends BaseActiveRecord
* You may also define default conditions that should apply to all queries unless overridden:
*
* ```php
* public static function createQuery($config = [])
* public static function createQuery()
* {
* return parent::createQuery($config)->where(['deleted' => false]);
* return parent::createQuery()->where(['deleted' => false]);
* }
* ```
*
* Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the
* default condition. Using [[Query::where()]] will override the default condition.
*
* @param array $config the configuration passed to the ActiveQuery class.
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new ActiveQuery($config);
return new ActiveQuery(get_called_class());
}
/**
......
......@@ -55,23 +55,20 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
* You may also define default conditions that should apply to all queries unless overridden:
*
* ```php
* public static function createQuery($config = [])
* public static function createQuery()
* {
* return parent::createQuery($config)->where(['deleted' => false]);
* return parent::createQuery()->where(['deleted' => false]);
* }
* ```
*
* Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the
* default condition. Using [[Query::where()]] will override the default condition.
*
* @param array $config the configuration passed to the ActiveQuery class.
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new ActiveQuery($config);
return new ActiveQuery(get_called_class());
}
/**
......
......@@ -59,23 +59,20 @@ class ActiveRecord extends BaseActiveRecord
* You may also define default conditions that should apply to all queries unless overridden:
*
* ```php
* public static function createQuery($config = [])
* public static function createQuery()
* {
* return parent::createQuery($config)->where(['deleted' => false]);
* return parent::createQuery()->where(['deleted' => false]);
* }
* ```
*
* Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the
* default condition. Using [[Query::where()]] will override the default condition.
*
* @param array $config the configuration passed to the ActiveQuery class.
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new ActiveQuery($config);
return new ActiveQuery(get_called_class());
}
/**
......
......@@ -146,23 +146,20 @@ abstract class ActiveRecord extends BaseActiveRecord
* You may also define default conditions that should apply to all queries unless overridden:
*
* ```php
* public static function createQuery($config = [])
* public static function createQuery()
* {
* return parent::createQuery($config)->where(['deleted' => false]);
* return parent::createQuery()->where(['deleted' => false]);
* }
* ```
*
* Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the
* default condition. Using [[Query::where()]] will override the default condition.
*
* @param array $config the configuration passed to the ActiveQuery class.
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new ActiveQuery($config);
return new ActiveQuery(get_called_class());
}
/**
......
......@@ -234,7 +234,8 @@ Yii Framework 2 Change Log
- Chg #2691: Null parameters will not be included in the generated URLs by `UrlManager` (gonimar, qiangxue)
- Chg #2734: `FileCache::keyPrefix` defaults to empty string now (qiangxue)
- Chg #2911: Removed `tbl_` default for table prefix (samdark)
_ Chg #2912: Relative view files will be looked for under the directory containing the view currently being rendered (qiangxue)
- Chg #2912: Relative view files will be looked for under the directory containing the view currently being rendered (qiangxue)
- Chg #2955: Changed the signature of ActiveQuery constructors and `ActiveRecord::createQuery()` to simplify customizing ActiveQuery classes (qiangxue)
- Chg: Renamed `yii\jui\Widget::clientEventsMap` to `clientEventMap` (qiangxue)
- Chg: Renamed `ActiveRecord::getPopulatedRelations()` to `getRelatedRecords()` (qiangxue)
- Chg: Renamed `attributeName` and `className` to `targetAttribute` and `targetClass` for `UniqueValidator` and `ExistValidator` (qiangxue)
......
......@@ -32,6 +32,17 @@ trait ActiveQueryTrait
/**
* Constructor.
* @param array $modelClass the model class associated with this query
* @param array $config configurations to be applied to the newly created query object
*/
public function __construct($modelClass, $config = [])
{
$this->modelClass = $modelClass;
parent::__construct($config);
}
/**
* Sets the [[asArray]] property.
* @param boolean $value whether to return the query results in terms of arrays instead of Active Records.
* @return static the query object itself
......
......@@ -234,23 +234,20 @@ class ActiveRecord extends BaseActiveRecord
* You may also define default conditions that should apply to all queries unless overridden:
*
* ```php
* public static function createQuery($config = [])
* public static function createQuery()
* {
* return parent::createQuery($config)->where(['deleted' => false]);
* return parent::createQuery()->where(['deleted' => false]);
* }
* ```
*
* Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the
* default condition. Using [[Query::where()]] will override the default condition.
*
* @param array $config the configuration passed to the ActiveQuery class.
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new ActiveQuery($config);
return new ActiveQuery(get_called_class());
}
/**
......
......@@ -155,19 +155,18 @@ interface ActiveRecordInterface
* You may also define default conditions that should apply to all queries unless overridden:
*
* ```php
* public static function createQuery($config = [])
* public static function createQuery()
* {
* return parent::createQuery($config)->where(['deleted' => false]);
* return parent::createQuery()->where(['deleted' => false]);
* }
* ```
*
* Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the
* default condition. Using [[Query::where()]] will override the default condition.
*
* @param array $config the configuration passed to the ActiveQuery class.
* @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance.
*/
public static function createQuery($config = []);
public static function createQuery();
/**
* Updates records using the provided attribute values and conditions.
......
......@@ -309,13 +309,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
public function hasOne($class, $link)
{
/** @var ActiveRecordInterface $class */
return $class::createQuery([
'modelClass' => $class,
'primaryModel' => $this,
'link' => $link,
'multiple' => false,
]);
/** @var ActiveQuery $query */
$query = $class::createQuery();
$query->primaryModel = $this;
$query->link = $link;
$query->multiple = false;
return $query;
}
/**
......@@ -351,13 +350,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
public function hasMany($class, $link)
{
/** @var ActiveRecordInterface $class */
return $class::createQuery([
'modelClass' => $class,
'primaryModel' => $this,
'link' => $link,
'multiple' => true,
]);
/** @var ActiveQuery $query */
$query = $class::createQuery();
$query->primaryModel = $this;
$query->link = $link;
$query->multiple = true;
return $query;
}
/**
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
use yii\base\Component;
/**
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ContentTypeNegotiator extends Component
{
/**
* @var array list of supported API version numbers. If the current request does not specify a version
* number, the first element will be used as the [[version|chosen version number]]. For this reason, you should
* put the latest version number at the first. If this property is empty, [[version]] will not be set.
*/
public $supportedVersions = [];
/**
* @var array list of supported response formats. The array keys are the requested content MIME types,
* and the array values are the corresponding response formats. The first element will be used
* as the response format if the current request does not specify a content type.
*/
public $supportedFormats = [
'application/json' => Response::FORMAT_JSON,
'application/xml' => Response::FORMAT_XML,
];
}
......@@ -62,10 +62,8 @@ class Customer extends ActiveRecord
parent::afterSave($insert);
}
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new CustomerQuery($config);
return new CustomerQuery(get_called_class());
}
}
......@@ -64,10 +64,8 @@ class Customer extends ActiveRecord
}
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new CustomerQuery($config);
return new CustomerQuery(get_called_class());
}
}
......@@ -25,10 +25,8 @@ class Customer extends ActiveRecord
return $this->hasMany(CustomerOrder::className(), ['customer_id' => '_id']);
}
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new CustomerQuery($config);
return new CustomerQuery(get_called_class());
}
}
......@@ -20,10 +20,8 @@ class CustomerFile extends ActiveRecord
);
}
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new CustomerFileQuery($config);
return new CustomerFileQuery(get_called_class());
}
}
......@@ -31,10 +31,8 @@ class Customer extends ActiveRecord
parent::afterSave($insert);
}
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new CustomerQuery($config);
return new CustomerQuery(get_called_class());
}
}
......@@ -25,10 +25,8 @@ class ArticleIndex extends ActiveRecord
return $this->source->content;
}
public static function createQuery($config = [])
public static function createQuery()
{
$config['modelClass'] = get_called_class();
return new ArticleIndexQuery($config);
return new ArticleIndexQuery(get_called_class());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment