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
a2fe1284
Commit
a2fe1284
authored
Nov 17, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored query and relation.
parent
684ee633
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
11 deletions
+12
-11
ActiveQuery.php
framework/yii/db/ActiveQuery.php
+2
-2
ActiveQueryTrait.php
framework/yii/db/ActiveQueryTrait.php
+7
-5
ActiveRelationTrait.php
framework/yii/db/ActiveRelationTrait.php
+3
-4
No files found.
framework/yii/db/ActiveQuery.php
View file @
a2fe1284
...
...
@@ -68,7 +68,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
if
(
!
empty
(
$rows
))
{
$models
=
$this
->
createModels
(
$rows
);
if
(
!
empty
(
$this
->
with
))
{
$this
->
populateRelations
(
$models
,
$this
->
with
);
$this
->
findWith
(
$this
->
with
,
$models
);
}
return
$models
;
}
else
{
...
...
@@ -98,7 +98,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
if
(
!
empty
(
$this
->
with
))
{
$models
=
[
$model
];
$this
->
populateRelations
(
$models
,
$this
->
with
);
$this
->
findWith
(
$this
->
with
,
$models
);
$model
=
$models
[
0
];
}
return
$model
;
...
...
framework/yii/db/ActiveQueryTrait.php
View file @
a2fe1284
...
...
@@ -21,7 +21,7 @@ trait ActiveQueryTrait
*/
public
$modelClass
;
/**
* @var array list of relations that this query should be performed with
* @var array
a
list of relations that this query should be performed with
*/
public
$with
;
/**
...
...
@@ -143,10 +143,12 @@ trait ActiveQueryTrait
}
/**
* @param ActiveRecord[] $models
* @param array $with
* Finds records corresponding to one or multiple relations and populates them into the primary models.
* @param array $with a list of relations that this query should be performed with. Please
* refer to [[with()]] for details about specifying this parameter.
* @param ActiveRecord[] $models the primary models
*/
p
rivate
function
populateRelations
(
&
$models
,
$with
)
p
ublic
function
findWith
(
$with
,
&
$models
)
{
$primaryModel
=
new
$this
->
modelClass
;
$relations
=
$this
->
normalizeRelations
(
$primaryModel
,
$with
);
...
...
@@ -155,7 +157,7 @@ trait ActiveQueryTrait
// inherit asArray from primary query
$relation
->
asArray
=
$this
->
asArray
;
}
$relation
->
findWith
(
$name
,
$models
);
$relation
->
populateRelation
(
$name
,
$models
);
}
}
...
...
framework/yii/db/ActiveRelationTrait.php
View file @
a2fe1284
...
...
@@ -73,13 +73,12 @@ trait ActiveRelationTrait
/**
* Finds the related records and populates them into the primary models.
* This method is internally used by [[ActiveQuery]]. Do not call it directly.
* @param string $name the relation name
* @param array $primaryModels primary models
* @return array the related models
* @throws InvalidConfigException
* @throws InvalidConfigException
if [[link]] is invalid
*/
public
function
findWith
(
$name
,
&
$primaryModels
)
public
function
populateRelation
(
$name
,
&
$primaryModels
)
{
if
(
!
is_array
(
$this
->
link
))
{
throw
new
InvalidConfigException
(
'Invalid link: it must be an array of key-value pairs.'
);
...
...
@@ -96,7 +95,7 @@ trait ActiveRelationTrait
/** @var ActiveRelationTrait $viaQuery */
list
(
$viaName
,
$viaQuery
)
=
$this
->
via
;
$viaQuery
->
primaryModel
=
null
;
$viaModels
=
$viaQuery
->
findWith
(
$viaName
,
$primaryModels
);
$viaModels
=
$viaQuery
->
populateRelation
(
$viaName
,
$primaryModels
);
$this
->
filterByModels
(
$viaModels
);
}
else
{
$this
->
filterByModels
(
$primaryModels
);
...
...
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