Commit 5d90e6da by Qiang Xue

removed scopes.

finished AR documentation.
parent cc33a930
Because [[ActiveQuery]] implements a set of query building methods,
additional query conditions can be specified by calling the methods of [[ActiveQuery]].
The returned [[ActiveQuery]] instance can be further customized by calling
methods defined in [[ActiveQuery]] before returning the populated active records.
Below are some examples:
......
......@@ -37,7 +37,6 @@ use yii\db\Exception;
* - [[with]]: list of relations that this query should be performed with.
* - [[indexBy]]: the name of the column by which the query result should be indexed.
* - [[asArray]]: whether to return each record as an array.
* - [[scopes]]: list of scopes that should be applied to this query.
*
* These options can be configured using methods of the same name. For example:
*
......@@ -69,32 +68,11 @@ class ActiveQuery extends Query
*/
public $asArray;
/**
* @var array list of scopes that should be applied to this query
*/
public $scopes;
/**
* @var string the SQL statement to be executed for retrieving AR records.
* This is set by [[ActiveRecord::findBySql()]].
*/
public $sql;
/**
* PHP magic method.
* This method is overridden so that scope methods declared in [[modelClass]]
* can be invoked as methods of ActiveQuery.
* @param string $name
* @param array $params
* @return mixed|ActiveQuery
*/
public function __call($name, $params)
{
if (method_exists($this->modelClass, $name)) {
$this->scopes[$name] = $params;
return $this;
} else {
return parent::__call($name, $params);
}
}
/**
* Executes query and returns all results as an array.
......@@ -179,9 +157,6 @@ class ActiveQuery extends Query
$tableName = $modelClass::tableName();
$this->from = array($tableName);
}
if (!empty($this->scopes)) {
$this->applyScopes($this->scopes);
}
/** @var $qb QueryBuilder */
$qb = $db->getQueryBuilder();
$this->sql = $qb->build($this);
......@@ -243,37 +218,6 @@ class ActiveQuery extends Query
return $this;
}
/**
* Specifies the scopes to be applied to this query.
*
* The parameters to this method can be either one or multiple strings, or a single array
* of scopes names and their corresponding customization parameters.
*
* The followings are some usage examples:
*
* ~~~
* // find all active customers
* Customer::find()->scopes('active')->all();
* // find active customers whose age is greater than 30
* Customer::find()->scopes(array(
* 'active',
* 'olderThan' => array(30),
* ))->all();
* // alternatively the above statement can be written as:
* Customer::find()->active()->olderThan(30)->all();
* ~~~
* @return ActiveQuery the query object itself
*/
public function scopes()
{
$this->scopes = func_get_args();
if (isset($this->scopes[0]) && is_array($this->scopes[0])) {
// the parameter is given as an array
$this->scopes = $this->scopes[0];
}
return $this;
}
private function createModels($rows)
{
$models = array();
......@@ -352,17 +296,4 @@ class ActiveQuery extends Query
}
return $relations;
}
private function applyScopes($scopes)
{
$modelClass = $this->modelClass;
foreach ($scopes as $name => $config) {
if (is_integer($name)) {
$modelClass::$config($this);
} else {
array_unshift($config, $this);
call_user_func_array(array($modelClass, $name), $config);
}
}
}
}
......@@ -20,7 +20,7 @@ use yii\db\Expression;
use yii\util\StringHelper;
/**
* ActiveRecord is the base class for classes representing relational data.
* ActiveRecord is the base class for classes representing relational data in terms of objects.
*
* @include @yii/db/ActiveRecord.md
*
......@@ -1127,9 +1127,10 @@ abstract class ActiveRecord extends Model
* @param ActiveRecord $model the model to be unlinked from the current one.
* @param boolean $delete whether to delete the model that contains the foreign key.
* If false, the model's foreign key will be set null and saved.
* If true, the model containing the foreign key will be deleted.
* @throws BadParamException if the models cannot be unlinked
*/
public function unlink($name, $model, $delete = true)
public function unlink($name, $model, $delete = false)
{
$relation = $this->getRelation($name);
......
......@@ -19,13 +19,4 @@ class Customer extends ActiveRecord
{
return $this->hasMany('Order', array('customer_id' => 'id'))->orderBy('id');
}
/**
* @param ActiveQuery $query
* @return ActiveQuery
*/
public static function active($query)
{
return $query->andWhere(array('status' => self::STATUS_ACTIVE));
}
}
\ No newline at end of file
......@@ -95,15 +95,6 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals('user2', $customer->name);
}
public function testScope()
{
$customers = Customer::find()->scopes('active')->all();
$this->assertEquals(2, count($customers));
$customers = Customer::find()->active()->all();
$this->assertEquals(2, count($customers));
}
public function testFindLazy()
{
/** @var $customer Customer */
......@@ -256,7 +247,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
// has many
$customer = Customer::find(2);
$this->assertEquals(2, count($customer->orders));
$customer->unlink('orders', $customer->orders[1]);
$customer->unlink('orders', $customer->orders[1], true);
$this->assertEquals(1, count($customer->orders));
$this->assertNull(Order::find(3));
......@@ -264,14 +255,14 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$order = Order::find(2);
$this->assertEquals(3, count($order->items));
$this->assertEquals(3, count($order->orderItems));
$order->unlink('items', $order->items[2]);
$order->unlink('items', $order->items[2], true);
$this->assertEquals(2, count($order->items));
$this->assertEquals(2, count($order->orderItems));
// via table
$order = Order::find(1);
$this->assertEquals(2, count($order->books));
$order->unlink('books', $order->books[1]);
$order->unlink('books', $order->books[1], true);
$this->assertEquals(1, count($order->books));
$this->assertEquals(1, count($order->orderItems));
}
......
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