clean up AR

This commit is contained in:
Qiang Xue
2013-01-13 10:23:03 -05:00
parent 46ab456f4e
commit 3ca50f3043
7 changed files with 113 additions and 393 deletions

View File

@@ -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);

View File

@@ -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 . '".');
}
/**

View File

@@ -1,102 +0,0 @@
<?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)
{
}
}