Fixes #4048: Added init event to ActiveQuery classes

This commit is contained in:
Qiang Xue
2014-07-19 20:26:58 -04:00
parent ab9d906351
commit e8c6bb516f
17 changed files with 116 additions and 6 deletions

View File

@ -77,6 +77,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface
use ActiveQueryTrait; use ActiveQueryTrait;
use ActiveRelationTrait; use ActiveRelationTrait;
/**
* @event Event an event that is triggered when the query is initialized via [[init()]].
*/
const EVENT_INIT = 'init';
/** /**
* Constructor. * Constructor.
@ -89,6 +94,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface
parent::__construct($config); 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. * Creates a DB command that can be used to execute this query.
* @param Connection $db the DB connection used to create the DB command. * @param Connection $db the DB connection used to create the DB command.

View File

@ -7,6 +7,7 @@
namespace yii\elasticsearch; namespace yii\elasticsearch;
use Yii;
use yii\base\InvalidCallException; use yii\base\InvalidCallException;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord; use yii\db\BaseActiveRecord;
@ -71,7 +72,7 @@ class ActiveRecord extends BaseActiveRecord
*/ */
public static function find() public static function find()
{ {
return new ActiveQuery(get_called_class()); return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
} }
/** /**

View File

@ -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) - 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 #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 #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 #4086: changedAttributes of afterSave Event now contain old values (dizews)
- Enh: Make error messages more readable in HTML output (cebe) - 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) - 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)

View File

@ -64,6 +64,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface
use ActiveQueryTrait; use ActiveQueryTrait;
use ActiveRelationTrait; use ActiveRelationTrait;
/**
* @event Event an event that is triggered when the query is initialized via [[init()]].
*/
const EVENT_INIT = 'init';
/** /**
* Constructor. * Constructor.
@ -76,6 +81,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface
parent::__construct($config); 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 * @inheritdoc
*/ */

View File

@ -7,6 +7,7 @@
namespace yii\mongodb; namespace yii\mongodb;
use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord; use yii\db\BaseActiveRecord;
use yii\db\StaleObjectException; use yii\db\StaleObjectException;
@ -96,7 +97,7 @@ abstract class ActiveRecord extends BaseActiveRecord
*/ */
public static function find() public static function find()
{ {
return new ActiveQuery(get_called_class()); return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
} }
/** /**

View File

@ -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 #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 #3778: Gii generator for Active Record model added (klimov-paul)
- Enh #3947: Migration support 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 #4086: changedAttributes of afterSave Event now contain old values (dizews)
- Enh #4335: `yii\mongodb\log\MongoDbTarget` log target added (klimov-paul) - Enh #4335: `yii\mongodb\log\MongoDbTarget` log target added (klimov-paul)

View File

@ -40,6 +40,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface
use ActiveQueryTrait; use ActiveQueryTrait;
use ActiveRelationTrait; use ActiveRelationTrait;
/**
* @event Event an event that is triggered when the query is initialized via [[init()]].
*/
const EVENT_INIT = 'init';
/** /**
* Constructor. * Constructor.
@ -52,6 +57,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface
parent::__construct($config); 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. * Executes query and returns all results as an array.
* @param \yii\mongodb\Connection $db the Mongo connection used to execute the query. * @param \yii\mongodb\Connection $db the Mongo connection used to execute the query.

View File

@ -7,6 +7,7 @@
namespace yii\mongodb\file; namespace yii\mongodb\file;
use Yii;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use yii\db\StaleObjectException; use yii\db\StaleObjectException;
use yii\web\UploadedFile; use yii\web\UploadedFile;
@ -49,7 +50,7 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
*/ */
public static function find() public static function find()
{ {
return new ActiveQuery(get_called_class()); return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
} }
/** /**

View File

@ -77,6 +77,10 @@ class ActiveQuery extends Component implements ActiveQueryInterface
use ActiveQueryTrait; use ActiveQueryTrait;
use ActiveRelationTrait; use ActiveRelationTrait;
/**
* @event Event an event that is triggered when the query is initialized via [[init()]].
*/
const EVENT_INIT = 'init';
/** /**
* Constructor. * Constructor.
@ -89,6 +93,18 @@ class ActiveQuery extends Component implements ActiveQueryInterface
parent::__construct($config); 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. * Executes the query and returns all results as an array.
* @param Connection $db the database connection used to execute the query. * @param Connection $db the database connection used to execute the query.

View File

@ -7,6 +7,7 @@
namespace yii\redis; namespace yii\redis;
use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord; use yii\db\BaseActiveRecord;
use yii\helpers\Inflector; use yii\helpers\Inflector;
@ -53,7 +54,7 @@ class ActiveRecord extends BaseActiveRecord
*/ */
public static function find() public static function find()
{ {
return new ActiveQuery(get_called_class()); return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
} }
/** /**

View File

@ -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 #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 #4086: changedAttributes of afterSave Event now contain old values (dizews)

View File

@ -85,6 +85,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface
use ActiveQueryTrait; use ActiveQueryTrait;
use ActiveRelationTrait; 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. * @var string the SQL statement to be executed for retrieving AR records.
* This is set by [[ActiveRecord::findBySql()]]. * This is set by [[ActiveRecord::findBySql()]].
@ -103,6 +108,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface
parent::__construct($config); 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 * Sets the [[snippetCallback]] to [[fetchSnippetSourceFromModels()]], which allows to
* fetch the snippet source strings from the Active Record models, using method * fetch the snippet source strings from the Active Record models, using method

View File

@ -140,7 +140,7 @@ abstract class ActiveRecord extends BaseActiveRecord
*/ */
public static function find() public static function find()
{ {
return new ActiveQuery(get_called_class()); return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
} }
/** /**

View File

@ -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 #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) - 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 #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 #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) - 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) - Chg #2287: Split `yii\sphinx\ColumnSchema::typecast()` into two methods `phpTypecast()` and `dbTypecast()` to allow specifying PDO type explicitly (cebe)

View File

@ -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. - 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 #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 #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) - Enh #4072: `\yii\rbac\PhpManager` adjustments (samdark)
- Data is now stored in three separate files for items, assignments and rules. File format is simpler. - Data is now stored in three separate files for items, assignments and rules. File format is simpler.
- Removed `authFile`. Added `itemFile`, `assignmentFile` and `ruleFile`. - Removed `authFile`. Added `itemFile`, `assignmentFile` and `ruleFile`.

View File

@ -72,6 +72,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface
use ActiveQueryTrait; use ActiveQueryTrait;
use ActiveRelationTrait; 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. * @var string the SQL statement to be executed for retrieving AR records.
* This is set by [[ActiveRecord::findBySql()]]. * This is set by [[ActiveRecord::findBySql()]].
@ -102,6 +107,18 @@ class ActiveQuery extends Query implements ActiveQueryInterface
parent::__construct($config); 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. * Executes query and returns all results as an array.
* @param Connection $db the DB connection used to create the DB command. * @param Connection $db the DB connection used to create the DB command.

View File

@ -258,7 +258,7 @@ class ActiveRecord extends BaseActiveRecord
*/ */
public static function find() public static function find()
{ {
return new ActiveQuery(get_called_class()); return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
} }
/** /**