diff --git a/extensions/elasticsearch/ActiveQuery.php b/extensions/elasticsearch/ActiveQuery.php index 5012761b89..21c9f69ccd 100644 --- a/extensions/elasticsearch/ActiveQuery.php +++ b/extensions/elasticsearch/ActiveQuery.php @@ -77,6 +77,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface use ActiveQueryTrait; use ActiveRelationTrait; + /** + * @event Event an event that is triggered when the query is initialized via [[init()]]. + */ + const EVENT_INIT = 'init'; + /** * Constructor. @@ -89,6 +94,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface parent::__construct($config); } + /** + * Initializes the object. + * This method is called at the end of the constructor. The default implementation will trigger + * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end + * to ensure triggering of the event. + */ + public function init() + { + parent::init(); + $this->trigger(self::EVENT_INIT); + } + /** * Creates a DB command that can be used to execute this query. * @param Connection $db the DB connection used to create the DB command. diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index 4c0ed983c1..f11d986217 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -7,6 +7,7 @@ namespace yii\elasticsearch; +use Yii; use yii\base\InvalidCallException; use yii\base\InvalidConfigException; use yii\db\BaseActiveRecord; @@ -71,7 +72,7 @@ class ActiveRecord extends BaseActiveRecord */ public static function find() { - return new ActiveQuery(get_called_class()); + return Yii::createObject(ActiveQuery::className(), [get_called_class()]); } /** diff --git a/extensions/elasticsearch/CHANGELOG.md b/extensions/elasticsearch/CHANGELOG.md index 22bbc46799..b9fd5f6d89 100644 --- a/extensions/elasticsearch/CHANGELOG.md +++ b/extensions/elasticsearch/CHANGELOG.md @@ -8,6 +8,7 @@ Yii Framework 2 elasticsearch extension Change Log - Bug #4187: Elasticsearch dynamic scripting is disabled in 1.2.0, so do not use it in query builder (cebe) - Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe) - Enh #3527: Added `highlight` property to Query and ActiveRecord. (Borales) +- Enh #4048: Added `init` event to `ActiveQuery` classes (qiangxue) - Enh #4086: changedAttributes of afterSave Event now contain old values (dizews) - Enh: Make error messages more readable in HTML output (cebe) - Chg: asArray in ActiveQuery is now equal to using the normal Query. This means, that the output structure has changed and `with` is supported anymore. (cebe) diff --git a/extensions/mongodb/ActiveQuery.php b/extensions/mongodb/ActiveQuery.php index dd1d196d8a..8a50d086f1 100644 --- a/extensions/mongodb/ActiveQuery.php +++ b/extensions/mongodb/ActiveQuery.php @@ -64,6 +64,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface use ActiveQueryTrait; use ActiveRelationTrait; + /** + * @event Event an event that is triggered when the query is initialized via [[init()]]. + */ + const EVENT_INIT = 'init'; + /** * Constructor. @@ -76,6 +81,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface parent::__construct($config); } + /** + * Initializes the object. + * This method is called at the end of the constructor. The default implementation will trigger + * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end + * to ensure triggering of the event. + */ + public function init() + { + parent::init(); + $this->trigger(self::EVENT_INIT); + } + /** * @inheritdoc */ diff --git a/extensions/mongodb/ActiveRecord.php b/extensions/mongodb/ActiveRecord.php index de029890b4..7b5653b86b 100644 --- a/extensions/mongodb/ActiveRecord.php +++ b/extensions/mongodb/ActiveRecord.php @@ -7,6 +7,7 @@ namespace yii\mongodb; +use Yii; use yii\base\InvalidConfigException; use yii\db\BaseActiveRecord; use yii\db\StaleObjectException; @@ -96,7 +97,7 @@ abstract class ActiveRecord extends BaseActiveRecord */ public static function find() { - return new ActiveQuery(get_called_class()); + return Yii::createObject(ActiveQuery::className(), [get_called_class()]); } /** diff --git a/extensions/mongodb/CHANGELOG.md b/extensions/mongodb/CHANGELOG.md index 00311361ee..ee2ebe99c0 100644 --- a/extensions/mongodb/CHANGELOG.md +++ b/extensions/mongodb/CHANGELOG.md @@ -9,6 +9,7 @@ Yii Framework 2 mongodb extension Change Log - Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe) - Enh #3778: Gii generator for Active Record model added (klimov-paul) - Enh #3947: Migration support added (klimov-paul) +- Enh #4048: Added `init` event to `ActiveQuery` classes (qiangxue) - Enh #4086: changedAttributes of afterSave Event now contain old values (dizews) - Enh #4335: `yii\mongodb\log\MongoDbTarget` log target added (klimov-paul) diff --git a/extensions/mongodb/file/ActiveQuery.php b/extensions/mongodb/file/ActiveQuery.php index 80d456e22c..77bb03931e 100644 --- a/extensions/mongodb/file/ActiveQuery.php +++ b/extensions/mongodb/file/ActiveQuery.php @@ -40,6 +40,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface use ActiveQueryTrait; use ActiveRelationTrait; + /** + * @event Event an event that is triggered when the query is initialized via [[init()]]. + */ + const EVENT_INIT = 'init'; + /** * Constructor. @@ -52,6 +57,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface parent::__construct($config); } + /** + * Initializes the object. + * This method is called at the end of the constructor. The default implementation will trigger + * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end + * to ensure triggering of the event. + */ + public function init() + { + parent::init(); + $this->trigger(self::EVENT_INIT); + } + /** * Executes query and returns all results as an array. * @param \yii\mongodb\Connection $db the Mongo connection used to execute the query. diff --git a/extensions/mongodb/file/ActiveRecord.php b/extensions/mongodb/file/ActiveRecord.php index 1a7f9bde97..1598b077c0 100644 --- a/extensions/mongodb/file/ActiveRecord.php +++ b/extensions/mongodb/file/ActiveRecord.php @@ -7,6 +7,7 @@ namespace yii\mongodb\file; +use Yii; use yii\base\InvalidParamException; use yii\db\StaleObjectException; use yii\web\UploadedFile; @@ -49,7 +50,7 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord */ public static function find() { - return new ActiveQuery(get_called_class()); + return Yii::createObject(ActiveQuery::className(), [get_called_class()]); } /** diff --git a/extensions/redis/ActiveQuery.php b/extensions/redis/ActiveQuery.php index 2a1aff6c1d..5c0b92100c 100644 --- a/extensions/redis/ActiveQuery.php +++ b/extensions/redis/ActiveQuery.php @@ -77,6 +77,10 @@ class ActiveQuery extends Component implements ActiveQueryInterface use ActiveQueryTrait; use ActiveRelationTrait; + /** + * @event Event an event that is triggered when the query is initialized via [[init()]]. + */ + const EVENT_INIT = 'init'; /** * Constructor. @@ -89,6 +93,18 @@ class ActiveQuery extends Component implements ActiveQueryInterface parent::__construct($config); } + /** + * Initializes the object. + * This method is called at the end of the constructor. The default implementation will trigger + * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end + * to ensure triggering of the event. + */ + public function init() + { + parent::init(); + $this->trigger(self::EVENT_INIT); + } + /** * Executes the query and returns all results as an array. * @param Connection $db the database connection used to execute the query. diff --git a/extensions/redis/ActiveRecord.php b/extensions/redis/ActiveRecord.php index aa7691fc48..6993126b9d 100644 --- a/extensions/redis/ActiveRecord.php +++ b/extensions/redis/ActiveRecord.php @@ -7,6 +7,7 @@ namespace yii\redis; +use Yii; use yii\base\InvalidConfigException; use yii\db\BaseActiveRecord; use yii\helpers\Inflector; @@ -53,7 +54,7 @@ class ActiveRecord extends BaseActiveRecord */ public static function find() { - return new ActiveQuery(get_called_class()); + return Yii::createObject(ActiveQuery::className(), [get_called_class()]); } /** diff --git a/extensions/redis/CHANGELOG.md b/extensions/redis/CHANGELOG.md index d80029907c..77df7bba99 100644 --- a/extensions/redis/CHANGELOG.md +++ b/extensions/redis/CHANGELOG.md @@ -5,6 +5,7 @@ Yii Framework 2 redis extension Change Log -------------------------- - Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe) +- Enh #4048: Added `init` event to `ActiveQuery` classes (qiangxue) - Enh #4086: changedAttributes of afterSave Event now contain old values (dizews) diff --git a/extensions/sphinx/ActiveQuery.php b/extensions/sphinx/ActiveQuery.php index f2e87d7520..ee3a405b5b 100644 --- a/extensions/sphinx/ActiveQuery.php +++ b/extensions/sphinx/ActiveQuery.php @@ -85,6 +85,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface use ActiveQueryTrait; use ActiveRelationTrait; + /** + * @event Event an event that is triggered when the query is initialized via [[init()]]. + */ + const EVENT_INIT = 'init'; + /** * @var string the SQL statement to be executed for retrieving AR records. * This is set by [[ActiveRecord::findBySql()]]. @@ -103,6 +108,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface parent::__construct($config); } + /** + * Initializes the object. + * This method is called at the end of the constructor. The default implementation will trigger + * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end + * to ensure triggering of the event. + */ + public function init() + { + parent::init(); + $this->trigger(self::EVENT_INIT); + } + /** * Sets the [[snippetCallback]] to [[fetchSnippetSourceFromModels()]], which allows to * fetch the snippet source strings from the Active Record models, using method diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index 97bf63b04a..7020f99279 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -140,7 +140,7 @@ abstract class ActiveRecord extends BaseActiveRecord */ public static function find() { - return new ActiveQuery(get_called_class()); + return Yii::createObject(ActiveQuery::className(), [get_called_class()]); } /** diff --git a/extensions/sphinx/CHANGELOG.md b/extensions/sphinx/CHANGELOG.md index 88eef3c071..dff9ecb733 100644 --- a/extensions/sphinx/CHANGELOG.md +++ b/extensions/sphinx/CHANGELOG.md @@ -7,6 +7,7 @@ Yii Framework 2 sphinx extension Change Log - Bug #3668: Escaping of the special characters at 'MATCH' statement added (klimov-paul) - Bug #4018: AR relation eager loading does not work with db models (klimov-paul) - Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe) +- Enh #4048: Added `init` event to `ActiveQuery` classes (qiangxue) - Enh #4086: changedAttributes of afterSave Event now contain old values (dizews) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Chg #2287: Split `yii\sphinx\ColumnSchema::typecast()` into two methods `phpTypecast()` and `dbTypecast()` to allow specifying PDO type explicitly (cebe) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 728aeeded0..610fd170dd 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -130,6 +130,7 @@ Yii Framework 2 Change Log - Added note about the fact that intl is required for non-latin languages to requirements checker. - Enh #3992: In mail layouts you can now access the message object via `$message` variable (qiangxue) - Enh #4028: Added ability to `yii\widgets\Menu` to encode each item's label separately (creocoder, umneeq) +- Enh #4048: Added `init` event to `ActiveQuery` classes (qiangxue) - Enh #4072: `\yii\rbac\PhpManager` adjustments (samdark) - Data is now stored in three separate files for items, assignments and rules. File format is simpler. - Removed `authFile`. Added `itemFile`, `assignmentFile` and `ruleFile`. diff --git a/framework/db/ActiveQuery.php b/framework/db/ActiveQuery.php index b928f31880..06778f7acc 100644 --- a/framework/db/ActiveQuery.php +++ b/framework/db/ActiveQuery.php @@ -72,6 +72,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface use ActiveQueryTrait; use ActiveRelationTrait; + /** + * @event Event an event that is triggered when the query is initialized via [[init()]]. + */ + const EVENT_INIT = 'init'; + /** * @var string the SQL statement to be executed for retrieving AR records. * This is set by [[ActiveRecord::findBySql()]]. @@ -102,6 +107,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface parent::__construct($config); } + /** + * Initializes the object. + * This method is called at the end of the constructor. The default implementation will trigger + * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end + * to ensure triggering of the event. + */ + public function init() + { + parent::init(); + $this->trigger(self::EVENT_INIT); + } + /** * Executes query and returns all results as an array. * @param Connection $db the DB connection used to create the DB command. diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 51ccaa4113..2025ca4de9 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -258,7 +258,7 @@ class ActiveRecord extends BaseActiveRecord */ public static function find() { - return new ActiveQuery(get_called_class()); + return Yii::createObject(ActiveQuery::className(), [get_called_class()]); } /**