Browse Source

fixed event doc.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
76b153a368
  1. 18
      docs/api/db/ActiveRecord.md
  2. 4
      framework/base/Application.php
  3. 85
      framework/base/Model.php
  4. 28
      framework/db/ActiveRecord.php
  5. 2
      framework/db/Connection.php
  6. 2
      framework/logging/Logger.php
  7. 8
      framework/logging/Router.php

18
docs/api/db/ActiveRecord.md

@ -375,27 +375,27 @@ method overriding and event handling mechanisms.
When instantiating a new ActiveRecord instance, we will have the following life cycles: When instantiating a new ActiveRecord instance, we will have the following life cycles:
1. constructor 1. constructor
2. [[init()]]: will trigger an [[init]] event 2. [[init()]]: will trigger an [[EVENT_INIT]] event
When getting an ActiveRecord instance through the [[find()]] method, we will have the following life cycles: When getting an ActiveRecord instance through the [[find()]] method, we will have the following life cycles:
1. constructor 1. constructor
2. [[init()]]: will trigger an [[init]] event 2. [[init()]]: will trigger an [[EVENT_INIT]] event
3. [[afterFind()]]: will trigger an [[afterFind]] event 3. [[afterFind()]]: will trigger an [[EVENT_AFTER_FIND]] event
When calling [[save()]] to insert or update an ActiveRecord, we will have the following life cycles: When calling [[save()]] to insert or update an ActiveRecord, we will have the following life cycles:
1. [[beforeValidate()]]: will trigger an [[beforeValidate]] event 1. [[beforeValidate()]]: will trigger an [[EVENT_BEFORE_VALIDATE]] event
2. [[beforeSave()]]: will trigger an [[beforeSave]] event 2. [[beforeSave()]]: will trigger an [[EVENT_BEFORE_INSERT]] or [[EVENT_BEFORE_UPDATE]] event
3. perform the actual data insertion or updating 3. perform the actual data insertion or updating
4. [[afterSave()]]: will trigger an [[afterSave]] event 4. [[afterSave()]]: will trigger an [[EVENT_AFTER_INSERT]] or [[EVENT_AFTER_UPDATE]] event
5. [[afterValidate()]]: will trigger an [[afterValidate]] event 5. [[afterValidate()]]: will trigger an [[EVENT_AFTER_VALIDATE]] event
Finally when calling [[delete()]] to delete an ActiveRecord, we will have the following life cycles: Finally when calling [[delete()]] to delete an ActiveRecord, we will have the following life cycles:
1. [[beforeDelete()]]: will trigger an [[beforeDelete]] event 1. [[beforeDelete()]]: will trigger an [[EVENT_BEFORE_DELETE]] event
2. perform the actual data deletion 2. perform the actual data deletion
3. [[afterDelete()]]: will trigger an [[afterDelete]] event 3. [[afterDelete()]]: will trigger an [[EVENT_AFTER_DELETE]] event
### Scopes ### Scopes

4
framework/base/Application.php

@ -176,7 +176,7 @@ class Application extends Module
} }
/** /**
* Raises the [[beforeRequest]] event right BEFORE the application processes the request. * Raises the [[EVENT_BEFORE_REQUEST]] event right BEFORE the application processes the request.
*/ */
public function beforeRequest() public function beforeRequest()
{ {
@ -184,7 +184,7 @@ class Application extends Module
} }
/** /**
* Raises the [[afterRequest]] event right AFTER the application processes the request. * Raises the [[EVENT_AFTER_REQUEST]] event right AFTER the application processes the request.
*/ */
public function afterRequest() public function afterRequest()
{ {

85
framework/base/Model.php

@ -26,8 +26,8 @@ use yii\validators\RequiredValidator;
* *
* Model also raises the following events when performing data validation: * Model also raises the following events when performing data validation:
* *
* - [[beforeValidate]]: an event raised at the beginning of [[validate()]] * - [[EVENT_BEFORE_VALIDATE]]: an event raised at the beginning of [[validate()]]
* - [[afterValidate]]: an event raised at the end of [[validate()]] * - [[EVENT_AFTER_VALIDATE]]: an event raised at the end of [[validate()]]
* *
* You may directly use Model to store model data, or extend it with customization. * You may directly use Model to store model data, or extend it with customization.
* You may also customize Model by attaching [[ModelBehavior|model behaviors]]. * You may also customize Model by attaching [[ModelBehavior|model behaviors]].
@ -53,9 +53,17 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/ */
const EVENT_AFTER_VALIDATE = 'afterValidate'; const EVENT_AFTER_VALIDATE = 'afterValidate';
private static $_attributes = array(); // class name => array of attribute names /**
private $_errors; // attribute name => array of errors * @var array validation errors (attribute name => array of errors)
private $_validators; // Vector of validators */
private $_errors;
/**
* @var Vector vector of validators
*/
private $_validators;
/**
* @var string current scenario
*/
private $_scenario = 'default'; private $_scenario = 'default';
/** /**
@ -68,10 +76,10 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
* *
* ~~~ * ~~~
* array( * array(
* 'attribute list', * 'attribute list',
* 'validator type', * 'validator type',
* 'on'=>'scenario name', * 'on'=>'scenario name',
* ...other parameters... * ...other parameters...
* ) * )
* ~~~ * ~~~
* *
@ -79,37 +87,37 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
* *
* - attribute list: required, specifies the attributes (separated by commas) to be validated; * - attribute list: required, specifies the attributes (separated by commas) to be validated;
* - validator type: required, specifies the validator to be used. It can be the name of a model * - validator type: required, specifies the validator to be used. It can be the name of a model
* class method, the name of a built-in validator, or a validator class name (or its path alias). * class method, the name of a built-in validator, or a validator class name (or its path alias).
* - on: optional, specifies the [[scenario|scenarios]] (separated by commas) when the validation * - on: optional, specifies the [[scenario|scenarios]] (separated by commas) when the validation
* rule can be applied. If this option is not set, the rule will apply to all scenarios. * rule can be applied. If this option is not set, the rule will apply to all scenarios.
* - additional name-value pairs can be specified to initialize the corresponding validator properties. * - additional name-value pairs can be specified to initialize the corresponding validator properties.
* Please refer to individual validator class API for possible properties. * Please refer to individual validator class API for possible properties.
* *
* A validator can be either an object of a class extending [[\yii\validators\Validator]], * A validator can be either an object of a class extending [[Validator]], or a model class method
* or a model class method (called *inline validator*) that has the following signature: * (called *inline validator*) that has the following signature:
* *
* ~~~ * ~~~
* // $params refers to validation parameters given in the rule * // $params refers to validation parameters given in the rule
* function validatorName($attribute, $params) * function validatorName($attribute, $params)
* ~~~ * ~~~
* *
* Yii also provides a set of [[\yii\validators\Validator::builtInValidators|built-in validators]]. * Yii also provides a set of [[Validator::builtInValidators|built-in validators]].
* They each has an alias name which can be used when specifying a validation rule. * They each has an alias name which can be used when specifying a validation rule.
* *
* Below are some examples: * Below are some examples:
* *
* ~~~ * ~~~
* array( * array(
* // built-in "required" validator * // built-in "required" validator
* array('username', 'required'), * array('username', 'required'),
* // built-in "length" validator customized with "min" and "max" properties * // built-in "length" validator customized with "min" and "max" properties
* array('username', 'length', 'min'=>3, 'max'=>12), * array('username', 'length', 'min'=>3, 'max'=>12),
* // built-in "compare" validator that is used in "register" scenario only * // built-in "compare" validator that is used in "register" scenario only
* array('password', 'compare', 'compareAttribute'=>'password2', 'on'=>'register'), * array('password', 'compare', 'compareAttribute'=>'password2', 'on'=>'register'),
* // an inline validator defined via the "authenticate()" method in the model class * // an inline validator defined via the "authenticate()" method in the model class
* array('password', 'authenticate', 'on'=>'login'), * array('password', 'authenticate', 'on'=>'login'),
* // a validator of class "CaptchaValidator" * // a validator of class "CaptchaValidator"
* array('captcha', 'CaptchaValidator'), * array('captcha', 'CaptchaValidator'),
* ); * );
* ~~~ * ~~~
* *
@ -151,8 +159,10 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
{ {
$attributes = array(); $attributes = array();
foreach ($this->getActiveValidators() as $validator) { foreach ($this->getActiveValidators() as $validator) {
foreach ($validator->attributes as $name) { if ($validator->isActive('default')) {
$attributes[$name] = true; foreach ($validator->attributes as $name) {
$attributes[$name] = true;
}
} }
} }
return array( return array(
@ -168,11 +178,6 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/ */
public function attributes() public function attributes()
{ {
$className = get_class($this);
if (isset(self::$_attributes[$className])) {
return self::$_attributes[$className];
}
$class = new \ReflectionClass($this); $class = new \ReflectionClass($this);
$names = array(); $names = array();
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
@ -181,7 +186,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
$names[] = $name; $names[] = $name;
} }
} }
return self::$_attributes[$className] = $names; return $names;
} }
/** /**
@ -395,13 +400,13 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
* *
* ~~~ * ~~~
* array( * array(
* 'username' => array( * 'username' => array(
* 'Username is required.', * 'Username is required.',
* 'Username must contain only word characters.', * 'Username must contain only word characters.',
* ), * ),
* 'email' => array( * 'email' => array(
* 'Email address is invalid.', * 'Email address is invalid.',
* ) * )
* ) * )
* ~~~ * ~~~
* *

28
framework/db/ActiveRecord.php

@ -608,8 +608,9 @@ class ActiveRecord extends Model
* 4. call [[afterSave()]]; * 4. call [[afterSave()]];
* 5. call [[afterValidate()]] when `$runValidation` is true. * 5. call [[afterValidate()]] when `$runValidation` is true.
* *
* In the above step 1, 2, 4 and 5, events named `beforeValidate`, `beforeInsert`, * In the above step 1, 2, 4 and 5, events [[EVENT_BEFORE_VALIDATE]],
* `afterInsert` and `afterValidate` will be raised by the corresponding methods. * [[EVENT_BEFORE_INSERT]], [[EVENT_AFTER_INSERT]] and [[EVENT_AFTER_VALIDATE]]
* will be raised by the corresponding methods.
* *
* Only the [[changedAttributes|changed attribute values]] will be inserted into database. * Only the [[changedAttributes|changed attribute values]] will be inserted into database.
* *
@ -678,8 +679,9 @@ class ActiveRecord extends Model
* 4. call [[afterSave()]]; * 4. call [[afterSave()]];
* 5. call [[afterValidate()]] when `$runValidation` is true. * 5. call [[afterValidate()]] when `$runValidation` is true.
* *
* In the above step 1, 2, 4 and 5, events named `beforeValidate`, `beforeUpdate`, * In the above step 1, 2, 4 and 5, events [[EVENT_BEFORE_VALIDATE]],
* `afterUpdate` and `afterValidate` will be raised by the corresponding methods. * [[EVENT_BEFORE_UPDATE]], [[EVENT_AFTER_UPDATE]] and [[EVENT_AFTER_VALIDATE]]
* will be raised by the corresponding methods.
* *
* Only the [[changedAttributes|changed attribute values]] will be saved into database. * Only the [[changedAttributes|changed attribute values]] will be saved into database.
* *
@ -760,7 +762,7 @@ class ActiveRecord extends Model
* 2. delete the record from the database; * 2. delete the record from the database;
* 3. call [[afterDelete()]]. * 3. call [[afterDelete()]].
* *
* In the above step 1 and 3, events named `beforeDelete` and `afterDelete` * In the above step 1 and 3, events named [[EVENT_BEFORE_DELETE]] and [[EVENT_AFTER_DELETE]]
* will be raised by the corresponding methods. * will be raised by the corresponding methods.
* *
* @return boolean whether the deletion is successful. * @return boolean whether the deletion is successful.
@ -791,7 +793,7 @@ class ActiveRecord extends Model
/** /**
* Initializes the object. * Initializes the object.
* This method is called at the end of the constructor. * This method is called at the end of the constructor.
* The default implementation will trigger an [[afterInsert]] event. * The default implementation will trigger an [[EVENT_INIT]] event.
* If you override this method, make sure you call the parent implementation at the end * If you override this method, make sure you call the parent implementation at the end
* to ensure triggering of the event. * to ensure triggering of the event.
*/ */
@ -803,7 +805,7 @@ class ActiveRecord extends Model
/** /**
* This method is called when the AR object is created and populated with the query result. * This method is called when the AR object is created and populated with the query result.
* The default implementation will trigger an [[afterFind]] event. * The default implementation will trigger an [[EVENT_AFTER_FIND]] event.
* When overriding this method, make sure you call the parent implementation to ensure the * When overriding this method, make sure you call the parent implementation to ensure the
* event is triggered. * event is triggered.
*/ */
@ -824,8 +826,8 @@ class ActiveRecord extends Model
/** /**
* This method is called at the beginning of inserting or updating a record. * This method is called at the beginning of inserting or updating a record.
* The default implementation will trigger a [[beforeInsert]] event when `$insert` is true, * The default implementation will trigger an [[EVENT_BEFORE_INSERT]] event when `$insert` is true,
* or a [[beforeUpdate]] event if `$insert` is false. * or an [[EVENT_BEFORE_UPDATE]] event if `$insert` is false.
* When overriding this method, make sure you call the parent implementation like the following: * When overriding this method, make sure you call the parent implementation like the following:
* *
* ~~~ * ~~~
@ -854,8 +856,8 @@ class ActiveRecord extends Model
/** /**
* This method is called at the end of inserting or updating a record. * This method is called at the end of inserting or updating a record.
* The default implementation will trigger an [[afterInsert]] event when `$insert` is true, * The default implementation will trigger an [[EVENT_AFTER_INSERT]] event when `$insert` is true,
* or an [[afterUpdate]] event if `$insert` is false. * or an [[EVENT_AFTER_UPDATE]] event if `$insert` is false.
* When overriding this method, make sure you call the parent implementation so that * When overriding this method, make sure you call the parent implementation so that
* the event is triggered. * the event is triggered.
* @param boolean $insert whether this method called while inserting a record. * @param boolean $insert whether this method called while inserting a record.
@ -868,7 +870,7 @@ class ActiveRecord extends Model
/** /**
* This method is invoked before deleting a record. * This method is invoked before deleting a record.
* The default implementation raises the [[beforeDelete]] event. * The default implementation raises the [[EVENT_BEFORE_DELETE]] event.
* When overriding this method, make sure you call the parent implementation like the following: * When overriding this method, make sure you call the parent implementation like the following:
* *
* ~~~ * ~~~
@ -894,7 +896,7 @@ class ActiveRecord extends Model
/** /**
* This method is invoked after deleting a record. * This method is invoked after deleting a record.
* The default implementation raises the [[afterDelete]] event. * The default implementation raises the [[EVENT_AFTER_DELETE]] event.
* You may override this method to do postprocessing after the record is deleted. * You may override this method to do postprocessing after the record is deleted.
* Make sure you call the parent implementation so that the event is raised properly. * Make sure you call the parent implementation so that the event is raised properly.
*/ */

2
framework/db/Connection.php

@ -375,7 +375,7 @@ class Connection extends Component
* This method is invoked right after the DB connection is established. * This method is invoked right after the DB connection is established.
* The default implementation turns on `PDO::ATTR_EMULATE_PREPARES` * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`
* if [[emulatePrepare]] is true, and sets the database [[charset]] if it is not empty. * if [[emulatePrepare]] is true, and sets the database [[charset]] if it is not empty.
* It then triggers an [[afterOpen]] event. * It then triggers an [[EVENT_AFTER_OPEN]] event.
*/ */
protected function initConnection() protected function initConnection()
{ {

2
framework/logging/Logger.php

@ -133,7 +133,7 @@ class Logger extends \yii\base\Component
/** /**
* Flushes log messages from memory to targets. * Flushes log messages from memory to targets.
* This method will trigger a [[flush]] or [[finalFlush]] event depending on the $final value. * This method will trigger an [[EVENT_FLUSH]] or [[EVENT_FINAL_FLUSH]] event depending on the $final value.
* @param boolean $final whether this is a final call during a request. * @param boolean $final whether this is a final call during a request.
*/ */
public function flush($final = false) public function flush($final = false)

8
framework/logging/Router.php

@ -69,8 +69,8 @@ class Router extends Component
/** /**
* Initializes this application component. * Initializes this application component.
* This method is invoked when the Router component is created by the application. * This method is invoked when the Router component is created by the application.
* The method attaches the [[processLogs]] method to both the [[Logger::flush]] event * The method attaches the [[processLogs]] method to both the [[Logger::EVENT_FLUSH]] event
* and the [[\yii\base\Application::afterRequest]] event. * and the [[Logger::EVENT_FINAL_FLUSH]] event.
*/ */
public function init() public function init()
{ {
@ -88,8 +88,8 @@ class Router extends Component
/** /**
* Retrieves and processes log messages from the system logger. * Retrieves and processes log messages from the system logger.
* This method mainly serves the event handler to [[Logger::flush]] * This method mainly serves the event handler to the [[Logger::EVENT_FLUSH]] event
* and [[Application::endRequest]] events. * and the [[Logger::EVENT_FINAL_FLUSH]] event.
* It will retrieve the available log messages from the [[Yii::getLogger()|system logger]] * It will retrieve the available log messages from the [[Yii::getLogger()|system logger]]
* and invoke the registered [[targets|log targets]] to do the actual processing. * and invoke the registered [[targets|log targets]] to do the actual processing.
* @param \yii\base\Event $event event parameter * @param \yii\base\Event $event event parameter

Loading…
Cancel
Save