Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
5d90e6da
Commit
5d90e6da
authored
Jan 14, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
removed scopes.
finished AR documentation.
parent
cc33a930
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
8 additions
and
95 deletions
+8
-95
ActiveRecord-find.md
docs/api/db/ActiveRecord-find.md
+2
-2
ActiveRecord.md
docs/api/db/ActiveRecord.md
+0
-0
ActiveQuery.php
framework/db/ActiveQuery.php
+0
-69
ActiveRecord.php
framework/db/ActiveRecord.php
+3
-2
Customer.php
tests/unit/data/ar/Customer.php
+0
-10
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+3
-12
No files found.
docs/api/db/ActiveRecord-find.md
View file @
5d90e6da
Because
[
[ActiveQuery
]
] implements a set of query building methods,
The returned
[
[ActiveQuery
]
] instance can be further customized by calling
additional query conditions can be specified by calling the methods of
[
[ActiveQuery
]
]
.
methods defined in
[
[ActiveQuery
]
] before returning the populated active records
.
Below are some examples:
Below are some examples:
...
...
docs/api/db/ActiveRecord.md
View file @
5d90e6da
This diff is collapsed.
Click to expand it.
framework/db/ActiveQuery.php
View file @
5d90e6da
...
@@ -37,7 +37,6 @@ use yii\db\Exception;
...
@@ -37,7 +37,6 @@ use yii\db\Exception;
* - [[with]]: list of relations that this query should be performed with.
* - [[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.
* - [[indexBy]]: the name of the column by which the query result should be indexed.
* - [[asArray]]: whether to return each record as an array.
* - [[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:
* These options can be configured using methods of the same name. For example:
*
*
...
@@ -69,32 +68,11 @@ class ActiveQuery extends Query
...
@@ -69,32 +68,11 @@ class ActiveQuery extends Query
*/
*/
public
$asArray
;
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.
* @var string the SQL statement to be executed for retrieving AR records.
* This is set by [[ActiveRecord::findBySql()]].
* This is set by [[ActiveRecord::findBySql()]].
*/
*/
public
$sql
;
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.
* Executes query and returns all results as an array.
...
@@ -179,9 +157,6 @@ class ActiveQuery extends Query
...
@@ -179,9 +157,6 @@ class ActiveQuery extends Query
$tableName
=
$modelClass
::
tableName
();
$tableName
=
$modelClass
::
tableName
();
$this
->
from
=
array
(
$tableName
);
$this
->
from
=
array
(
$tableName
);
}
}
if
(
!
empty
(
$this
->
scopes
))
{
$this
->
applyScopes
(
$this
->
scopes
);
}
/** @var $qb QueryBuilder */
/** @var $qb QueryBuilder */
$qb
=
$db
->
getQueryBuilder
();
$qb
=
$db
->
getQueryBuilder
();
$this
->
sql
=
$qb
->
build
(
$this
);
$this
->
sql
=
$qb
->
build
(
$this
);
...
@@ -243,37 +218,6 @@ class ActiveQuery extends Query
...
@@ -243,37 +218,6 @@ class ActiveQuery extends Query
return
$this
;
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
)
private
function
createModels
(
$rows
)
{
{
$models
=
array
();
$models
=
array
();
...
@@ -352,17 +296,4 @@ class ActiveQuery extends Query
...
@@ -352,17 +296,4 @@ class ActiveQuery extends Query
}
}
return
$relations
;
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
);
}
}
}
}
}
framework/db/ActiveRecord.php
View file @
5d90e6da
...
@@ -20,7 +20,7 @@ use yii\db\Expression;
...
@@ -20,7 +20,7 @@ use yii\db\Expression;
use
yii\util\StringHelper
;
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
* @include @yii/db/ActiveRecord.md
*
*
...
@@ -1127,9 +1127,10 @@ abstract class ActiveRecord extends Model
...
@@ -1127,9 +1127,10 @@ abstract class ActiveRecord extends Model
* @param ActiveRecord $model the model to be unlinked from the current one.
* @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.
* @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 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
* @throws BadParamException if the models cannot be unlinked
*/
*/
public
function
unlink
(
$name
,
$model
,
$delete
=
tru
e
)
public
function
unlink
(
$name
,
$model
,
$delete
=
fals
e
)
{
{
$relation
=
$this
->
getRelation
(
$name
);
$relation
=
$this
->
getRelation
(
$name
);
...
...
tests/unit/data/ar/Customer.php
View file @
5d90e6da
...
@@ -19,13 +19,4 @@ class Customer extends ActiveRecord
...
@@ -19,13 +19,4 @@ class Customer extends ActiveRecord
{
{
return
$this
->
hasMany
(
'Order'
,
array
(
'customer_id'
=>
'id'
))
->
orderBy
(
'id'
);
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
tests/unit/framework/db/ActiveRecordTest.php
View file @
5d90e6da
...
@@ -95,15 +95,6 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -95,15 +95,6 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$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
()
public
function
testFindLazy
()
{
{
/** @var $customer Customer */
/** @var $customer Customer */
...
@@ -256,7 +247,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -256,7 +247,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
// has many
// has many
$customer
=
Customer
::
find
(
2
);
$customer
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
$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
->
assertEquals
(
1
,
count
(
$customer
->
orders
));
$this
->
assertNull
(
Order
::
find
(
3
));
$this
->
assertNull
(
Order
::
find
(
3
));
...
@@ -264,14 +255,14 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -264,14 +255,14 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$order
=
Order
::
find
(
2
);
$order
=
Order
::
find
(
2
);
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$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
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
// via table
// via table
$order
=
Order
::
find
(
1
);
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
2
,
count
(
$order
->
books
));
$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
->
books
));
$this
->
assertEquals
(
1
,
count
(
$order
->
orderItems
));
$this
->
assertEquals
(
1
,
count
(
$order
->
orderItems
));
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment