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
50743935
Commit
50743935
authored
Mar 01, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
a53acdf6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
99 additions
and
106 deletions
+99
-106
ActiveFinder.php
framework/db/ar/ActiveFinder.php
+1
-5
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+1
-70
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+4
-3
ActiveRelation.php
framework/db/ar/ActiveRelation.php
+1
-28
BaseActiveQuery.php
framework/db/ar/BaseActiveQuery.php
+81
-0
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+11
-0
No files found.
framework/db/ar/ActiveFinder.php
View file @
50743935
...
...
@@ -68,7 +68,6 @@ class ActiveFinder extends \yii\base\Object
}
else
{
$this
->
initFrom
(
$query
);
$this
->
applyScopes
(
$query
);
// todo: filter
$sql
=
$this
->
connection
->
getQueryBuilder
()
->
build
(
$query
);
if
(
strpos
(
$sql
,
'@.'
)
!==
false
)
{
if
(
$query
->
tableAlias
!==
null
)
{
...
...
@@ -189,10 +188,7 @@ class ActiveFinder extends \yii\base\Object
{
if
(
is_array
(
$query
->
scopes
))
{
$class
=
$query
->
modelClass
;
$defaultScope
=
$class
::
defaultScope
();
if
(
$defaultScope
!==
null
)
{
call_user_func_array
(
$defaultScope
,
array
(
$query
));
}
$class
::
defaultScope
(
$query
);
$scopes
=
$class
::
scopes
();
foreach
(
$query
->
scopes
as
$name
=>
$params
)
{
if
(
is_integer
(
$name
))
{
...
...
framework/db/ar/ActiveQuery.php
View file @
50743935
...
...
@@ -11,7 +11,6 @@
namespace
yii\db\ar
;
use
yii\base\VectorIterator
;
use
yii\db\dao\BaseQuery
;
use
yii\db\dao\Expression
;
use
yii\db\Exception
;
...
...
@@ -39,39 +38,9 @@ use yii\db\Exception;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ActiveQuery
extends
BaseQuery
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
class
ActiveQuery
extends
Base
Active
Query
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
{
/**
* @var string the name of the ActiveRecord class.
*/
public
$modelClass
;
/**
* @var array list of relations that this query should be performed with
*/
public
$with
;
/**
* @var array list of relations that should be used as filters for this query.
*/
public
$filters
;
/**
* @var string the table alias to be used for query
*/
public
$tableAlias
;
/**
* @var string the name of the column that the result should be indexed by.
* This is only useful when the query result is returned as an array.
*/
public
$indexBy
;
/**
* @var boolean whether to return each record as an array. If false (default), an object
* of [[modelClass]] will be created to represent each record.
*/
public
$asArray
;
/**
* @var array list of scopes that should be applied to this query
*/
public
$scopes
;
/**
* @var string the SQL statement to be executed to retrieve primary records.
* This is set by [[ActiveRecord::findBySql()]].
*/
...
...
@@ -101,44 +70,6 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
}
}
public
function
asArray
(
$value
=
true
)
{
$this
->
asArray
=
$value
;
return
$this
;
}
public
function
with
()
{
$this
->
with
=
func_get_args
();
if
(
isset
(
$this
->
with
[
0
])
&&
is_array
(
$this
->
with
[
0
]))
{
// the parameter is given as an array
$this
->
with
=
$this
->
with
[
0
];
}
return
$this
;
}
public
function
filters
()
{
$this
->
filters
=
func_get_args
();
if
(
isset
(
$this
->
filters
[
0
])
&&
is_array
(
$this
->
filters
[
0
]))
{
// the parameter is given as an array
$this
->
filters
=
$this
->
filters
[
0
];
}
return
$this
;
}
public
function
indexBy
(
$column
)
{
$this
->
indexBy
=
$column
;
return
$this
;
}
public
function
tableAlias
(
$value
)
{
$this
->
tableAlias
=
$value
;
return
$this
;
}
/**
* Executes query and returns all results as an array.
* @return array the query results. If the query results in nothing, an empty array will be returned.
...
...
framework/db/ar/ActiveRecord.php
View file @
50743935
...
...
@@ -364,12 +364,13 @@ abstract class ActiveRecord extends Model
* Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries.
* The default implementation simply returns an empty array. You may override this method
* if the model needs to be queried with some default criteria (e.g. only active records should be returned).
* @return array the query criteria. This will be used as the parameter to the constructor
* @param BaseActiveQuery
* @return BaseActiveQuery the query criteria. This will be used as the parameter to the constructor
* of {@link CDbCriteria}.
*/
public
static
function
defaultScope
()
public
static
function
defaultScope
(
$query
)
{
return
array
()
;
return
$query
;
}
/**
...
...
framework/db/ar/ActiveRelation.php
View file @
50743935
...
...
@@ -10,25 +10,19 @@
namespace
yii\db\ar
;
use
yii\db\dao\BaseQuery
;
/**
* ActiveRelation represents the specification of a relation declared in [[ActiveRecord::relations()]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ActiveRelation
extends
BaseQuery
class
ActiveRelation
extends
Base
Active
Query
{
/**
* @var string the name of this relation
*/
public
$name
;
/**
* @var string the name of the model class that this relation represents
*/
public
$modelClass
;
/**
* @var boolean whether this relation is a one-many relation
*/
public
$hasMany
;
...
...
@@ -38,15 +32,6 @@ class ActiveRelation extends BaseQuery
*/
public
$joinType
;
/**
* @var string the table alias used for the corresponding table during query
*/
public
$tableAlias
;
/**
* @var string the name of the column that the result should be indexed by.
* This is only useful when [[hasMany]] is true.
*/
public
$indexBy
;
/**
* @var string the ON clause of the join query
*/
public
$on
;
...
...
@@ -54,16 +39,4 @@ class ActiveRelation extends BaseQuery
* @var string
*/
public
$via
;
/**
* @var array the relations that should be queried together (eager loading)
*/
public
$with
;
/**
* @var array the relations that should be used as filters for this query
*/
public
$filters
;
/**
* @var array the scopes that should be applied during query
*/
public
$scopes
;
}
framework/db/ar/BaseActiveQuery.php
0 → 100644
View file @
50743935
<?php
/**
* ActiveFinder class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\db\ar
;
use
yii\db\dao\BaseQuery
;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
BaseActiveQuery
extends
BaseQuery
{
/**
* @var string the name of the ActiveRecord class.
*/
public
$modelClass
;
/**
* @var array list of relations that this query should be performed with
*/
public
$with
;
/**
* @var string the table alias to be used for query
*/
public
$tableAlias
;
/**
* @var string the name of the column that the result should be indexed by.
* This is only useful when the query result is returned as an array.
*/
public
$indexBy
;
/**
* @var boolean whether to return each record as an array. If false (default), an object
* of [[modelClass]] will be created to represent each record.
*/
public
$asArray
;
/**
* @var array list of scopes that should be applied to this query
*/
public
$scopes
;
public
function
asArray
(
$value
=
true
)
{
$this
->
asArray
=
$value
;
return
$this
;
}
public
function
with
()
{
$this
->
with
=
func_get_args
();
if
(
isset
(
$this
->
with
[
0
])
&&
is_array
(
$this
->
with
[
0
]))
{
// the parameter is given as an array
$this
->
with
=
$this
->
with
[
0
];
}
return
$this
;
}
public
function
indexBy
(
$column
)
{
$this
->
indexBy
=
$column
;
return
$this
;
}
public
function
tableAlias
(
$value
)
{
$this
->
tableAlias
=
$value
;
return
$this
;
}
public
function
scopes
(
$names
)
{
$this
->
scopes
=
$names
;
return
$this
;
}
}
tests/unit/framework/db/ar/ActiveRecordTest.php
View file @
50743935
...
...
@@ -208,6 +208,17 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
// scopes
$customers
=
Customer
::
find
()
->
active
()
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$customers
=
Customer
::
find
(
array
(
'scopes'
=>
array
(
'active'
),
))
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
// asArray
$customers
=
Customer
::
find
()
->
orderBy
(
'id'
)
->
asArray
()
->
all
();
$this
->
assertEquals
(
'user2'
,
$customers
[
1
][
'name'
]);
// indexBy
$customers
=
Customer
::
find
()
->
orderBy
(
'id'
)
->
indexBy
(
'name'
)
->
all
();
}
public
function
testEagerLoading
()
...
...
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