Qiang Xue
13 years ago
14 changed files with 208 additions and 189 deletions
@ -0,0 +1,79 @@
|
||||
Component provides the *event* and *behavior* features, in addition to the *property* feature which is implemented in |
||||
its parent class [[Object]]. |
||||
|
||||
Event is a way to "inject" custom code into existing code at certain places. For example, a comment object can trigger |
||||
an "add" event when the user adds a comment. We can write custom code and attach it to this event so that when the event |
||||
is triggered, our custom code will be executed. |
||||
|
||||
An event is identified by a name (unique within the class it is defined). Event names are *case-sensitive*. |
||||
|
||||
An event can be attached with one or multiple PHP callbacks, called *event handlers*. One can call [[trigger()]] to |
||||
raise an event. When an event is raised, the attached event handlers will be invoked automatically in the order they are |
||||
attached to the event. |
||||
|
||||
To attach an event handler to an event, call [[on()]]. For example, |
||||
|
||||
~~~ |
||||
$comment->on('add', function($event) { |
||||
// send email notification |
||||
}); |
||||
~~~ |
||||
|
||||
In the above, we attach an anonymous function to the "add" event of the comment. Valid event handlers include: |
||||
|
||||
- anonymous function: `function($event) { ... }` |
||||
- object method: `array($object, 'handleAdd')` |
||||
- static method: `array('Page', 'handleAdd')` |
||||
- global function: `'handleAdd'` |
||||
|
||||
The signature of an event handler should be like the following: |
||||
|
||||
~~~ |
||||
function foo($event) |
||||
~~~ |
||||
|
||||
where `$event` is an [[Event]] object which includes parameters associated with the event. |
||||
|
||||
One can also attach an event handler to an event when configuring a component with a configuration array. The syntax is |
||||
like the following: |
||||
|
||||
~~~ |
||||
array( |
||||
'on add' => function($event) { ... } |
||||
) |
||||
~~~ |
||||
|
||||
where `on add` stands for attaching an event to the `add` event. |
||||
|
||||
One can call [[getEventHandlers()]] to retrieve all event handlers that are attached to a specified event. Because this |
||||
method returns a [[Vector]] object, we can manipulate this object to attach/detach event handlers, or adjust their |
||||
relative orders. |
||||
|
||||
~~~ |
||||
$handlers = $comment->getEventHandlers('add'); |
||||
$handlers->insertAt(0, $callback); // attach a handler as the first one |
||||
$handlers[] = $callback; // attach a handler as the last one |
||||
unset($handlers[0]); // detach the first handler |
||||
~~~ |
||||
|
||||
|
||||
A behavior is an instance of [[Behavior]] or its child class. A component can be attached with one or multiple |
||||
behaviors. When a behavior is attached to a component, its public properties and methods can be accessed via the |
||||
component directly, as if the component owns those properties and methods. |
||||
|
||||
To attach a behavior to a component, declare it in [[behaviors()]], or explicitly call [[attachBehavior]]. Behaviors |
||||
declared in [[behaviors()]] are automatically attached to the corresponding component. |
||||
|
||||
One can also attach a behavior to a component when configuring it with a configuration array. The syntax is like the |
||||
following: |
||||
|
||||
~~~ |
||||
array( |
||||
'as tree' => array( |
||||
'class' => 'Tree', |
||||
), |
||||
) |
||||
~~~ |
||||
|
||||
where `as tree` stands for attaching a behavior named `tree`, and the array will be passed to [[\Yii::createObject()]] |
||||
to create the behavior object. |
@ -0,0 +1,39 @@
|
||||
A property is defined by a getter method (e.g. `getLabel`), and/or a setter method (e.g. `setLabel`). For example, |
||||
the following getter and setter methods define a property named `label`: |
||||
|
||||
~~~ |
||||
private $_label; |
||||
|
||||
public function getLabel() |
||||
{ |
||||
return $this->_label; |
||||
} |
||||
|
||||
public function setLabel($value) |
||||
{ |
||||
$this->_label = $value; |
||||
} |
||||
~~~ |
||||
|
||||
Property names are *case-insensitive*. |
||||
|
||||
A property can be accessed like a member variable of an object. Reading or writing a property will cause the invocation |
||||
of the corresponding getter or setter method. For example, |
||||
|
||||
~~~ |
||||
// equivalent to $label = $object->getLabel(); |
||||
$label = $object->label; |
||||
// equivalent to $object->setLabel('abc'); |
||||
$object->label = 'abc'; |
||||
~~~ |
||||
|
||||
If a property has only a getter method and has no setter method, it is considered as *read-only*. In this case, trying |
||||
to modify the property value will cause an exception. |
||||
|
||||
One can call [[hasProperty]], [[canGetProperty]] and/or [[canSetProperty]] to check the existence of a property. |
||||
|
||||
Besides the property feature, the Object class defines a static method [[create]] which provides a convenient |
||||
alternative way of creating a new object instance. |
||||
|
||||
The Object class also defines the [[evaluateExpression]] method so that a PHP expression or callback can be dynamically |
||||
evaluated within the context of an object. |
@ -1,115 +1,102 @@
|
||||
<?php |
||||
/** |
||||
* CActiveRecordBehavior class file. |
||||
* ActiveRecordBehavior class file. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright © 2008-2011 Yii Software LLC |
||||
* @copyright Copyright © 2008-2012 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db\ar; |
||||
|
||||
use yii\base\ModelBehavior; |
||||
|
||||
/** |
||||
* CActiveRecordBehavior is the base class for behaviors that can be attached to {@link CActiveRecord}. |
||||
* Compared with {@link CModelBehavior}, CActiveRecordBehavior attaches to more events |
||||
* that are only defined by {@link CActiveRecord}. |
||||
* ActiveRecordBehavior is the base class for behaviors that can be attached to [[ActiveRecord]]. |
||||
* |
||||
* Compared to [[\yii\base\ModelBehavior]], ActiveRecordBehavior responds to more events |
||||
* that are specific to [[ActiveRecord]]. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class CActiveRecordBehavior extends CModelBehavior |
||||
class ActiveRecordBehavior extends ModelBehavior |
||||
{ |
||||
/** |
||||
* Declares events and the corresponding event handler methods. |
||||
* If you override this method, make sure you merge the parent result to the return value. |
||||
* @return array events (array keys) and the corresponding event handler methods (array values). |
||||
* @see CBehavior::events |
||||
* @see \yii\base\Behavior::events() |
||||
*/ |
||||
public function events() |
||||
{ |
||||
return array_merge(parent::events(), array( |
||||
'onBeforeInsert' => 'beforeInsert', |
||||
'onAfterInsert' => 'afterInsert', |
||||
'onBeforeUpdate' => 'beforeUpdate', |
||||
'onAfterUpdate' => 'afterUpdate', |
||||
'onBeforeDelete' => 'beforeDelete', |
||||
'onAfterDelete' => 'afterDelete', |
||||
'onBeforeFind' => 'beforeFind', |
||||
'onAfterFind' => 'afterFind', |
||||
'beforeInsert' => 'beforeInsert', |
||||
'afterInsert' => 'afterInsert', |
||||
'beforeUpdate' => 'beforeUpdate', |
||||
'afterUpdate' => 'afterUpdate', |
||||
'beforeDelete' => 'beforeDelete', |
||||
'afterDelete' => 'afterDelete', |
||||
)); |
||||
} |
||||
|
||||
/** |
||||
* Responds to {@link CActiveRecord::onBeforeSave} event. |
||||
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. |
||||
* You may set {@link CModelEvent::isValid} to be false to quit the saving process. |
||||
* @param CModelEvent $event event parameter |
||||
* Responds to the owner's `beforeInsert` event. |
||||
* Overrides this method if you want to handle the corresponding event of the owner. |
||||
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter |
||||
* to be false to quit the ActiveRecord inserting process. |
||||
* @param \yii\base\ModelEvent $event event parameter |
||||
*/ |
||||
public function beforeInsert($event) |
||||
{ |
||||
} |
||||
|
||||
/** |
||||
* Responds to {@link CActiveRecord::onAfterSave} event. |
||||
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. |
||||
* @param CModelEvent $event event parameter |
||||
* Responds to the owner's `afterInsert` event. |
||||
* Overrides this method if you want to handle the corresponding event of the owner. |
||||
* @param \yii\base\ModelEvent $event event parameter |
||||
*/ |
||||
public function afterInsert($event) |
||||
{ |
||||
} |
||||
|
||||
/** |
||||
* Responds to {@link CActiveRecord::onBeforeSave} event. |
||||
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. |
||||
* You may set {@link CModelEvent::isValid} to be false to quit the saving process. |
||||
* @param CModelEvent $event event parameter |
||||
* Responds to the owner's `beforeUpdate` event. |
||||
* Overrides this method if you want to handle the corresponding event of the owner. |
||||
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter |
||||
* to be false to quit the ActiveRecord updating process. |
||||
* @param \yii\base\ModelEvent $event event parameter |
||||
*/ |
||||
public function beforeUpdate($event) |
||||
{ |
||||
} |
||||
|
||||
/** |
||||
* Responds to {@link CActiveRecord::onAfterSave} event. |
||||
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. |
||||
* @param CModelEvent $event event parameter |
||||
* Responds to the owner's `afterUpdate` event. |
||||
* Overrides this method if you want to handle the corresponding event of the owner. |
||||
* @param \yii\base\ModelEvent $event event parameter |
||||
*/ |
||||
public function afterUpdate($event) |
||||
{ |
||||
} |
||||
|
||||
/** |
||||
* Responds to {@link CActiveRecord::onBeforeDelete} event. |
||||
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. |
||||
* You may set {@link CModelEvent::isValid} to be false to quit the deletion process. |
||||
* @param CEvent $event event parameter |
||||
* Responds to the owner's `beforeDelete` event. |
||||
* Overrides this method if you want to handle the corresponding event of the owner. |
||||
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter |
||||
* to be false to quit the ActiveRecord deleting process. |
||||
* @param \yii\base\ModelEvent $event event parameter |
||||
*/ |
||||
public function beforeDelete($event) |
||||
{ |
||||
} |
||||
|
||||
/** |
||||
* Responds to {@link CActiveRecord::onAfterDelete} event. |
||||
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. |
||||
* @param CEvent $event event parameter |
||||
* Responds to the owner's `afterDelete` event. |
||||
* Overrides this method if you want to handle the corresponding event of the owner. |
||||
* @param \yii\base\ModelEvent $event event parameter |
||||
*/ |
||||
public function afterDelete($event) |
||||
{ |
||||
} |
||||
|
||||
/** |
||||
* Responds to {@link CActiveRecord::onBeforeFind} event. |
||||
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. |
||||
* @param CEvent $event event parameter |
||||
*/ |
||||
public function beforeFind($event) |
||||
{ |
||||
} |
||||
|
||||
/** |
||||
* Responds to {@link CActiveRecord::onAfterFind} event. |
||||
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. |
||||
* @param CEvent $event event parameter |
||||
*/ |
||||
public function afterFind($event) |
||||
{ |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue