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
a2edf01c
Commit
a2edf01c
authored
Mar 01, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Delay joinWith till createCommand to correctly combine relation condition with primary condition.
parent
15b75941
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
12 deletions
+48
-12
ActiveQuery.php
framework/db/ActiveQuery.php
+28
-12
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+20
-0
No files found.
framework/db/ActiveQuery.php
View file @
a2edf01c
...
...
@@ -86,6 +86,10 @@ class ActiveQuery extends Query implements ActiveQueryInterface
* @see onCondition()
*/
public
$on
;
/**
* @var array a list of relations that this query should be joined with
*/
public
$joinWith
;
/**
...
...
@@ -232,6 +236,10 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/
protected
function
createCommandInternal
(
$db
)
{
if
(
!
empty
(
$this
->
joinWith
))
{
$this
->
buildJoinWith
();
}
/** @var ActiveRecord $modelClass */
$modelClass
=
$this
->
modelClass
;
if
(
$db
===
null
)
{
...
...
@@ -330,24 +338,32 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/
public
function
joinWith
(
$with
,
$eagerLoading
=
true
,
$joinType
=
'LEFT JOIN'
)
{
$with
=
(
array
)
$with
;
$this
->
joinWithRelations
(
new
$this
->
modelClass
,
$with
,
$joinType
);
$this
->
joinWith
[]
=
[(
array
)
$with
,
$eagerLoading
,
$joinType
];
return
$this
;
}
if
(
is_array
(
$eagerLoading
))
{
foreach
(
$with
as
$name
=>
$callback
)
{
if
(
is_integer
(
$name
))
{
if
(
!
in_array
(
$callback
,
$eagerLoading
,
true
))
{
private
function
buildJoinWith
()
{
foreach
(
$this
->
joinWith
as
$config
)
{
list
(
$with
,
$eagerLoading
,
$joinType
)
=
$config
;
$this
->
joinWithRelations
(
new
$this
->
modelClass
,
$with
,
$joinType
);
if
(
is_array
(
$eagerLoading
))
{
foreach
(
$with
as
$name
=>
$callback
)
{
if
(
is_integer
(
$name
))
{
if
(
!
in_array
(
$callback
,
$eagerLoading
,
true
))
{
unset
(
$with
[
$name
]);
}
}
elseif
(
!
in_array
(
$name
,
$eagerLoading
,
true
))
{
unset
(
$with
[
$name
]);
}
}
elseif
(
!
in_array
(
$name
,
$eagerLoading
,
true
))
{
unset
(
$with
[
$name
]);
}
}
elseif
(
!
$eagerLoading
)
{
$with
=
[];
}
}
elseif
(
!
$eagerLoading
)
{
$with
=
[];
}
return
$this
->
with
(
$with
);
$this
->
with
(
$with
);
}
}
/**
...
...
tests/unit/framework/db/ActiveRecordTest.php
View file @
a2edf01c
...
...
@@ -258,6 +258,16 @@ class ActiveRecordTest extends DatabaseTestCase
$this
->
assertTrue
(
$orders
[
0
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$orders
[
1
]
->
isRelationPopulated
(
'customer'
));
// inner join filtering, eager loading, conditions on both primary and relation
$orders
=
Order
::
find
()
->
innerJoinWith
([
'customer'
=>
function
(
$query
)
{
$query
->
where
([
'tbl_customer.id'
=>
2
]);
},
])
->
where
([
'tbl_order.id'
=>
[
1
,
2
]])
->
orderBy
(
'tbl_order.id'
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
id
);
$this
->
assertTrue
(
$orders
[
0
]
->
isRelationPopulated
(
'customer'
));
// inner join filtering without eager loading
$orders
=
Order
::
find
()
->
innerJoinWith
([
'customer'
=>
function
(
$query
)
{
...
...
@@ -270,6 +280,16 @@ class ActiveRecordTest extends DatabaseTestCase
$this
->
assertFalse
(
$orders
[
0
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertFalse
(
$orders
[
1
]
->
isRelationPopulated
(
'customer'
));
// inner join filtering without eager loading, conditions on both primary and relation
$orders
=
Order
::
find
()
->
innerJoinWith
([
'customer'
=>
function
(
$query
)
{
$query
->
where
([
'tbl_customer.id'
=>
2
]);
},
],
false
)
->
where
([
'tbl_order.id'
=>
[
1
,
2
]])
->
orderBy
(
'tbl_order.id'
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
id
);
$this
->
assertFalse
(
$orders
[
0
]
->
isRelationPopulated
(
'customer'
));
// join with via-relation
$orders
=
Order
::
find
()
->
innerJoinWith
(
'books'
)
->
orderBy
(
'tbl_order.id'
)
->
all
();
$this
->
assertEquals
(
2
,
count
(
$orders
));
...
...
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