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
795a09c2
Commit
795a09c2
authored
Jan 05, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1791: joinWithRelation using table alias.
parent
a4f8e828
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
11 deletions
+33
-11
ActiveQuery.php
framework/yii/db/ActiveQuery.php
+19
-11
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+14
-0
No files found.
framework/yii/db/ActiveQuery.php
View file @
795a09c2
...
...
@@ -326,11 +326,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
/**
* Returns the table
alias (or table name) that can be used to prefix column names
.
* Returns the table
name and the table alias for [[modelClass]]
.
* @param ActiveQuery $query
* @return
string the table alias (or table name) enclosed within double curly bracket
s.
* @return
array the table name and the table alia
s.
*/
private
function
getQueryTable
Alias
(
$query
)
private
function
getQueryTable
Name
(
$query
)
{
if
(
empty
(
$query
->
from
))
{
/** @var ActiveRecord $modelClass */
...
...
@@ -341,12 +341,12 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
if
(
preg_match
(
'/^(.*?)\s+({{\w+}}|\w+)$/'
,
$tableName
,
$matches
))
{
$tableName
=
$matches
[
2
];
}
if
(
strpos
(
$tableName
,
'{{'
)
===
false
)
{
$tableName
=
'{{'
.
$tableName
.
'}}'
;
$alias
=
$matches
[
2
];
}
else
{
$alias
=
$tableName
;
}
return
$tableName
;
return
[
$tableName
,
$alias
];
}
/**
...
...
@@ -372,13 +372,21 @@ class ActiveQuery extends Query implements ActiveQueryInterface
return
;
}
$parentTable
=
$this
->
getQueryTableAlias
(
$parent
);
$childTable
=
$this
->
getQueryTableAlias
(
$child
);
list
(
$parentTable
,
$parentAlias
)
=
$this
->
getQueryTableName
(
$parent
);
list
(
$childTable
,
$childAlias
)
=
$this
->
getQueryTableName
(
$child
);
if
(
!
empty
(
$child
->
link
))
{
if
(
strpos
(
$parentAlias
,
'{{'
)
===
false
)
{
$parentAlias
=
'{{'
.
$parentAlias
.
'}}'
;
}
if
(
strpos
(
$childAlias
,
'{{'
)
===
false
)
{
$childAlias
=
'{{'
.
$childAlias
.
'}}'
;
}
$on
=
[];
foreach
(
$child
->
link
as
$childColumn
=>
$parentColumn
)
{
$on
[]
=
"
$parent
Table
.[[
$parentColumn
]] =
$childTable
.[[
$childColumn
]]"
;
$on
[]
=
"
$parent
Alias
.[[
$parentColumn
]] =
$childAlias
.[[
$childColumn
]]"
;
}
$on
=
implode
(
' AND '
,
$on
);
}
else
{
...
...
tests/unit/framework/db/ActiveRecordTest.php
View file @
795a09c2
...
...
@@ -276,5 +276,19 @@ class ActiveRecordTest extends DatabaseTestCase
$this
->
assertEquals
(
3
,
count
(
$orders
[
0
]
->
items
));
$this
->
assertTrue
(
$orders
[
0
]
->
items
[
0
]
->
isRelationPopulated
(
'category'
));
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
items
[
0
]
->
category
->
id
);
// join with table alias
$orders
=
Order
::
find
()
->
joinWith
([
'customer'
=>
function
(
$q
)
{
$q
->
from
(
'tbl_customer c'
);
}
])
->
orderBy
(
'c.id DESC, tbl_order.id'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
id
);
$this
->
assertEquals
(
3
,
$orders
[
1
]
->
id
);
$this
->
assertEquals
(
1
,
$orders
[
2
]
->
id
);
$this
->
assertTrue
(
$orders
[
0
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$orders
[
1
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$orders
[
2
]
->
isRelationPopulated
(
'customer'
));
}
}
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