Browse Source

....

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
a53acdf60d
  1. 17
      framework/db/ar/ActiveFinder.php
  2. 12
      framework/db/ar/ActiveQuery.php
  3. 12
      tests/unit/data/ar/Customer.php
  4. 7
      tests/unit/data/mysql.sql
  5. 11
      tests/unit/framework/db/ar/ActiveRecordTest.php

17
framework/db/ar/ActiveFinder.php

@ -188,13 +188,22 @@ class ActiveFinder extends \yii\base\Object
protected function applyScopes($query) protected function applyScopes($query)
{ {
if (is_array($query->scopes)) { if (is_array($query->scopes)) {
foreach ($query->scopes as $scope => $params) { $class = $query->modelClass;
if (is_integer($scope)) { $defaultScope = $class::defaultScope();
$scope = $params; if ($defaultScope !== null) {
call_user_func_array($defaultScope, array($query));
}
$scopes = $class::scopes();
foreach ($query->scopes as $name => $params) {
if (is_integer($name)) {
$name = $params;
$params = array(); $params = array();
} }
if (!isset($scopes[$name])) {
throw new Exception("$class has no scope named '$name'.");
}
array_unshift($params, $query); array_unshift($params, $query);
call_user_func_array($scope, $params); call_user_func_array($scopes[$name], $params);
} }
} }
} }

12
framework/db/ar/ActiveQuery.php

@ -89,6 +89,18 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
$this->modelClass = $modelClass; $this->modelClass = $modelClass;
} }
public function __call($name, $params)
{
$class = $this->modelClass;
$scopes = $class::scopes();
if (isset($scopes[$name])) {
array_unshift($params, $this);
return call_user_func_array($scopes[$name], $params);
} else {
return parent::__call($name, $params);
}
}
public function asArray($value = true) public function asArray($value = true)
{ {
$this->asArray = $value; $this->asArray = $value;

12
tests/unit/data/ar/Customer.php

@ -4,6 +4,9 @@ namespace yiiunit\data\ar;
class Customer extends ActiveRecord class Customer extends ActiveRecord
{ {
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 2;
public static function tableName() public static function tableName()
{ {
return 'tbl_customer'; return 'tbl_customer';
@ -17,4 +20,13 @@ class Customer extends ActiveRecord
), ),
); );
} }
public static function scopes()
{
return array(
'active' => function($q) {
return $q->andWhere('@.status = 1');
},
);
}
} }

7
tests/unit/data/mysql.sql

@ -19,6 +19,7 @@ CREATE TABLE `tbl_customer` (
`email` varchar(128) NOT NULL, `email` varchar(128) NOT NULL,
`name` varchar(128) NOT NULL, `name` varchar(128) NOT NULL,
`address` text, `address` text,
`status` int (11) DEFAULT 0,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@ -71,9 +72,9 @@ CREATE TABLE `tbl_type` (
`bool_col2` tinyint(1) DEFAULT '1' `bool_col2` tinyint(1) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO tbl_customer (email, name, address) VALUES ('user1@example.com', 'user1', 'address1'); INSERT INTO tbl_customer (email, name, address, status) VALUES ('user1@example.com', 'user1', 'address1', 1);
INSERT INTO tbl_customer (email, name, address) VALUES ('user2@example.com', 'user2', 'address2'); INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1);
INSERT INTO tbl_customer (email, name, address) VALUES ('user3@example.com', 'user3', 'address3'); INSERT INTO tbl_customer (email, name, address, status) VALUES ('user3@example.com', 'user3', 'address3', 2);
INSERT INTO tbl_category (name) VALUES ('Books'); INSERT INTO tbl_category (name) VALUES ('Books');
INSERT INTO tbl_category (name) VALUES ('Movies'); INSERT INTO tbl_category (name) VALUES ('Movies');

11
tests/unit/framework/db/ar/ActiveRecordTest.php

@ -158,7 +158,12 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals('user2', $customer->name); $this->assertEquals('user2', $customer->name);
// find count // find count
$this->assertEquals(3, Customer::find()->count(true)); $this->assertEquals(3, Customer::find()->count());
$this->assertEquals(3, Customer::count());
$this->assertEquals(1, Customer::count(2));
$this->assertEquals(2, Customer::count(array(
'where' => 'id=1 OR id=2',
)));
} }
public function testFindBySql() public function testFindBySql()
@ -199,6 +204,10 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertTrue($customer instanceof Customer); $this->assertTrue($customer instanceof Customer);
$this->assertEquals(3, $customer->id); $this->assertEquals(3, $customer->id);
$this->assertEquals(null, $customer->name); $this->assertEquals(null, $customer->name);
// scopes
$customers = Customer::find()->active()->all();
$this->assertEquals(2, count($customers));
} }
public function testEagerLoading() public function testEagerLoading()

Loading…
Cancel
Save