Commit 3ca50f30 by Qiang Xue

clean up AR

parent 46ab456f
- `beforeInsert`. Raised before the record is saved.
By setting [[\yii\base\ModelEvent::isValid]] to be false, the normal [[save()]] will be stopped.
- `afterInsert`. Raised after the record is saved.
- `beforeUpdate`. Raised before the record is saved.
By setting [[\yii\base\ModelEvent::isValid]] to be false, the normal [[save()]] will be stopped.
- `afterUpdate`. Raised after the record is saved.
- `beforeDelete`. Raised before the record is deleted.
By setting [[\yii\base\ModelEvent::isValid]] to be false, the normal [[delete()]] process will be stopped.
- `afterDelete`. Raised after the record is deleted.
<?php
/**
* ModelBehavior class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* ModelBehavior class.
*
* ModelBehavior is a base class for behaviors that are attached to a model object.
* The model should be an instance of [[Model]] or its child classes.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ModelBehavior extends Behavior
{
/**
* Declares event handlers for the owner's events.
* The default implementation returns the following event handlers:
*
* - `beforeValidate` event
* - `afterValidate` event
*
* You may override these event handler methods to respond to the corresponding owner events.
* @return array events (array keys) and the corresponding event handler methods (array values).
*/
public function events()
{
return array(
'beforeValidate' => 'beforeValidate',
'afterValidate' => 'afterValidate',
);
}
/**
* Responds to the owner's `beforeValidate` event.
* Override this method if you want to handle the `beforeValidate` event of the [[owner]].
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter
* to be false to cancel the validation process.
* @param ModelEvent $event event parameter
*/
public function beforeValidate($event)
{
}
/**
* Responds to the owner's `afterValidate` event.
* Override this method if you want to handle the `beforeValidate` event of the [[owner]].
* @param Event $event event parameter
*/
public function afterValidate($event)
{
}
}
......@@ -68,7 +68,7 @@ class ActiveQuery extends Query
if ($rows !== array()) {
$models = $this->createModels($rows);
if (!empty($this->with)) {
$this->fetchRelatedModels($models, $this->with);
$this->populateRelations($models, $this->with);
}
return $models;
} else {
......@@ -92,7 +92,7 @@ class ActiveQuery extends Query
$model = $class::create($row);
if (!empty($this->with)) {
$models = array($model);
$this->fetchRelatedModels($models, $this->with);
$this->populateRelations($models, $this->with);
$model = $models[0];
}
return $model;
......@@ -216,7 +216,7 @@ class ActiveQuery extends Query
return $models;
}
protected function fetchRelatedModels(&$models, $with)
protected function populateRelations(&$models, $with)
{
$primaryModel = new $this->modelClass;
$relations = $this->normalizeRelations($primaryModel, $with);
......
......@@ -11,9 +11,9 @@
namespace yii\db;
use yii\base\Model;
use yii\base\Event;
use yii\base\ModelEvent;
use yii\base\BadMethodException;
use yii\base\BadParamException;
use yii\db\Exception;
use yii\db\Connection;
use yii\db\TableSchema;
......@@ -23,23 +23,12 @@ use yii\util\StringHelper;
/**
* ActiveRecord is the base class for classes representing relational data.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @include @yii/db/ActiveRecord.md
*
* @property array $attributes attribute values indexed by attribute names
*
* ActiveRecord provides a set of events for further customization:
*
* - `beforeInsert`. Raised before the record is saved.
* By setting [[\yii\base\ModelEvent::isValid]] to be false, the normal [[save()]] will be stopped.
* - `afterInsert`. Raised after the record is saved.
* - `beforeUpdate`. Raised before the record is saved.
* By setting [[\yii\base\ModelEvent::isValid]] to be false, the normal [[save()]] will be stopped.
* - `afterUpdate`. Raised after the record is saved.
* - `beforeDelete`. Raised before the record is deleted.
* By setting [[\yii\base\ModelEvent::isValid]] to be false, the normal [[delete()]] process will be stopped.
* - `afterDelete`. Raised after the record is deleted.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
abstract class ActiveRecord extends Model
{
......@@ -620,9 +609,6 @@ abstract class ActiveRecord extends Model
*/
public function updateCounters($counters)
{
if ($this->getIsNewRecord()) {
throw new Exception('The active record cannot be updated because it is new.');
}
$this->updateAllCounters($counters, $this->getOldPrimaryKey(true));
foreach ($counters as $name => $value) {
$this->_attributes[$name] += $value;
......@@ -787,7 +773,8 @@ abstract class ActiveRecord extends Model
/**
* Creates an active record with the given attributes.
* Note that this method does not save the record into database.
* This method is called by [[ActiveQuery]] to populate the query results
* into Active Records.
* @param array $row attribute values (name => value)
* @return ActiveRecord the newly created active record.
*/
......@@ -833,9 +820,12 @@ abstract class ActiveRecord extends Model
}
/**
* @param string $name
* @return ActiveRelation
* @throws Exception
* Returns the relation object with the specified name.
* A relation is defined by a getter method which returns an [[ActiveRelation]] object.
* It can be declared in either the Active Record class itself or one of its behaviors.
* @param string $name the relation name
* @return ActiveRelation the relation object
* @throws BadParamException if the named relation does not exist.
*/
public function getRelation($name)
{
......@@ -847,7 +837,7 @@ abstract class ActiveRecord extends Model
}
} catch (BadMethodException $e) {
}
throw new Exception('Unknown relation: ' . $name);
throw new BadParamException(get_class($this) . ' has no relation named "' . $name . '".');
}
/**
......
<?php
/**
* ActiveRecordBehavior class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db;
use yii\base\ModelBehavior;
/**
* 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 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 \yii\base\Behavior::events()
*/
public function events()
{
return array_merge(parent::events(), array(
'beforeInsert' => 'beforeInsert',
'afterInsert' => 'afterInsert',
'beforeUpdate' => 'beforeUpdate',
'afterUpdate' => 'afterUpdate',
'beforeDelete' => 'beforeDelete',
'afterDelete' => 'afterDelete',
));
}
/**
* 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 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 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 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 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 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)
{
}
}
......@@ -8,6 +8,8 @@ class Customer extends ActiveRecord
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 2;
public $status2;
public static function tableName()
{
return 'tbl_customer';
......
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