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
b90f94fe
Commit
b90f94fe
authored
Mar 14, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finished eager loading.
parent
c50d5872
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
5 deletions
+56
-5
ActiveFinder.php
framework/db/ar/ActiveFinder.php
+46
-1
QueryBuilder.php
framework/db/dao/QueryBuilder.php
+5
-2
TableSchema.php
framework/db/dao/TableSchema.php
+2
-2
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+3
-0
No files found.
framework/db/ar/ActiveFinder.php
View file @
b90f94fe
...
@@ -120,6 +120,11 @@ class ActiveFinder extends \yii\base\Object
...
@@ -120,6 +120,11 @@ class ActiveFinder extends \yii\base\Object
$q
=
new
Query
;
$q
=
new
Query
;
$this
->
buildJoinQuery
(
$joinTree
,
$q
);
$this
->
buildJoinQuery
(
$joinTree
,
$q
);
if
(
$this
->
_hasMany
&&
(
$query
->
limit
>
0
||
$query
->
offset
>
0
))
{
$this
->
limitQuery
(
$query
,
$q
);
}
$rows
=
$q
->
createCommand
(
$this
->
connection
)
->
queryAll
();
$rows
=
$q
->
createCommand
(
$this
->
connection
)
->
queryAll
();
foreach
(
$rows
as
$row
)
{
foreach
(
$rows
as
$row
)
{
$joinTree
->
createRecord
(
$row
);
$joinTree
->
createRecord
(
$row
);
...
@@ -202,7 +207,7 @@ class ActiveFinder extends \yii\base\Object
...
@@ -202,7 +207,7 @@ class ActiveFinder extends \yii\base\Object
}
elseif
(
is_array
(
$relation
->
via
))
{
}
elseif
(
is_array
(
$relation
->
via
))
{
// join via a pivoting table
// join via a pivoting table
$r
=
new
ActiveRelation
;
$r
=
new
ActiveRelation
;
$r
->
name
=
'
p
t'
.
$this
->
_joinCount
;
$r
->
name
=
'
v
t'
.
$this
->
_joinCount
;
$r
->
hasMany
=
$relation
->
hasMany
;
$r
->
hasMany
=
$relation
->
hasMany
;
foreach
(
$relation
->
via
as
$name
=>
$value
)
{
foreach
(
$relation
->
via
as
$name
=>
$value
)
{
...
@@ -288,6 +293,8 @@ class ActiveFinder extends \yii\base\Object
...
@@ -288,6 +293,8 @@ class ActiveFinder extends \yii\base\Object
$quotedPrefixes
=
array
(
$quotedPrefixes
=
array
(
'@.'
=>
$this
->
connection
->
quoteTableName
(
$element
->
query
->
tableAlias
,
true
)
.
'.'
,
'@.'
=>
$this
->
connection
->
quoteTableName
(
$element
->
query
->
tableAlias
,
true
)
.
'.'
,
);
);
$query
->
limit
=
$element
->
query
->
limit
;
$query
->
offset
=
$element
->
query
->
offset
;
}
}
$qb
=
$this
->
connection
->
getQueryBuilder
();
$qb
=
$this
->
connection
->
getQueryBuilder
();
...
@@ -436,4 +443,41 @@ class ActiveFinder extends \yii\base\Object
...
@@ -436,4 +443,41 @@ class ActiveFinder extends \yii\base\Object
return
$columns
;
return
$columns
;
}
}
protected
function
limitQuery
(
$activeQuery
,
$query
)
{
$q
=
clone
$query
;
$modelClass
=
$activeQuery
->
modelClass
;
$table
=
$modelClass
::
getMetaData
()
->
table
;
$q
->
select
=
array
();
foreach
(
$table
->
primaryKey
as
$name
)
{
$q
->
select
[]
=
$alias
=
$activeQuery
->
tableAlias
.
'.'
.
$name
;
}
$q
->
distinct
=
true
;
$rows
=
$q
->
createCommand
(
$this
->
connection
)
->
queryAll
();
if
(
count
(
$table
->
primaryKey
)
===
1
)
{
$name
=
$table
->
primaryKey
[
0
];
$values
=
array
();
foreach
(
$rows
as
$row
)
{
$values
[]
=
$table
->
columns
[
$name
]
->
typecast
(
$row
[
$name
]);
}
$query
->
andWhere
(
array
(
'in'
,
$activeQuery
->
tableAlias
.
'.'
.
$name
,
$values
));
}
else
{
$ors
=
array
(
'or'
);
$prefix
=
$this
->
connection
->
quoteTableName
(
$activeQuery
->
tableAlias
,
true
)
.
'.'
;
foreach
(
$rows
as
$row
)
{
$ands
=
array
();
foreach
(
$table
->
primaryKey
as
$name
)
{
$value
=
$table
->
columns
[
$name
]
->
typecast
(
$row
[
$name
]);
if
(
is_string
(
$value
))
{
$value
=
$this
->
connection
->
quoteValue
(
$value
);
}
$ands
[]
=
$prefix
.
$this
->
connection
->
quoteColumnName
(
$name
,
true
)
.
'='
.
$value
;
}
$ors
[]
=
implode
(
' AND '
,
$ands
);
}
$query
->
andWhere
(
$ors
);
}
$query
->
limit
=
$query
->
offset
=
null
;
}
}
}
\ No newline at end of file
framework/db/dao/QueryBuilder.php
View file @
b90f94fe
...
@@ -533,6 +533,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -533,6 +533,7 @@ class QueryBuilder extends \yii\base\Object
}
}
}
}
if
(
$parts
!==
array
())
{
if
(
$parts
!==
array
())
{
$operator
=
strtoupper
(
$operator
);
return
'('
.
implode
(
")
$operator
("
,
$parts
)
.
')'
;
return
'('
.
implode
(
")
$operator
("
,
$parts
)
.
')'
;
}
else
{
}
else
{
return
''
;
return
''
;
...
@@ -580,6 +581,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -580,6 +581,7 @@ class QueryBuilder extends \yii\base\Object
$column
=
$this
->
quoteColumnName
(
$column
);
$column
=
$this
->
quoteColumnName
(
$column
);
}
}
$operator
=
strtoupper
(
$operator
);
return
"
$column
$operator
("
.
implode
(
', '
,
$values
)
.
')'
;
return
"
$column
$operator
("
.
implode
(
', '
,
$values
)
.
')'
;
}
}
...
@@ -600,9 +602,9 @@ class QueryBuilder extends \yii\base\Object
...
@@ -600,9 +602,9 @@ class QueryBuilder extends \yii\base\Object
}
}
if
(
$operator
===
'like'
||
$operator
===
'not like'
)
{
if
(
$operator
===
'like'
||
$operator
===
'not like'
)
{
$andor
=
'
and
'
;
$andor
=
'
AND
'
;
}
else
{
}
else
{
$andor
=
'
or
'
;
$andor
=
'
OR
'
;
$operator
=
$operator
===
'or like'
?
'like'
:
'not like'
;
$operator
=
$operator
===
'or like'
?
'like'
:
'not like'
;
}
}
...
@@ -610,6 +612,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -610,6 +612,7 @@ class QueryBuilder extends \yii\base\Object
$column
=
$this
->
quoteColumnName
(
$column
);
$column
=
$this
->
quoteColumnName
(
$column
);
}
}
$operator
=
strtoupper
(
$operator
);
$parts
=
array
();
$parts
=
array
();
foreach
(
$values
as
$value
)
{
foreach
(
$values
as
$value
)
{
$parts
[]
=
"
$column
$operator
"
.
$this
->
connection
->
quoteValue
(
$value
);
$parts
[]
=
"
$column
$operator
"
.
$this
->
connection
->
quoteValue
(
$value
);
...
...
framework/db/dao/TableSchema.php
View file @
b90f94fe
...
@@ -41,7 +41,7 @@ class TableSchema extends \yii\base\Object
...
@@ -41,7 +41,7 @@ class TableSchema extends \yii\base\Object
*/
*/
public
$quotedName
;
public
$quotedName
;
/**
/**
* @var
array
primary keys of this table.
* @var
string[]
primary keys of this table.
*/
*/
public
$primaryKey
=
array
();
public
$primaryKey
=
array
();
/**
/**
...
@@ -61,7 +61,7 @@ class TableSchema extends \yii\base\Object
...
@@ -61,7 +61,7 @@ class TableSchema extends \yii\base\Object
*/
*/
public
$foreignKeys
=
array
();
public
$foreignKeys
=
array
();
/**
/**
* @var
array
column metadata of this table. Each array element is a [[ColumnSchema]] object, indexed by column names.
* @var
ColumnSchema[]
column metadata of this table. Each array element is a [[ColumnSchema]] object, indexed by column names.
*/
*/
public
$columns
=
array
();
public
$columns
=
array
();
...
...
tests/unit/framework/db/ar/ActiveRecordTest.php
View file @
b90f94fe
...
@@ -250,6 +250,9 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -250,6 +250,9 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertEquals
(
1
,
$orders
[
0
]
->
books
[
0
]
->
id
);
$this
->
assertEquals
(
1
,
$orders
[
0
]
->
books
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
books
[
1
]
->
id
);
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
books
[
1
]
->
id
);
$this
->
assertEquals
(
2
,
$orders
[
1
]
->
books
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$orders
[
1
]
->
books
[
0
]
->
id
);
$orders
=
Order
::
find
()
->
with
(
'items'
)
->
order
(
'@.id'
)
->
limit
(
2
)
->
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