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
5ddc1ba1
Commit
5ddc1ba1
authored
Mar 25, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished AR query.
parent
23318399
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
51 deletions
+65
-51
ActiveFinder.php
framework/db/ar/ActiveFinder.php
+22
-12
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+2
-1
JoinElement.php
framework/db/ar/JoinElement.php
+2
-2
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+39
-36
No files found.
framework/db/ar/ActiveFinder.php
View file @
5ddc1ba1
...
...
@@ -19,7 +19,6 @@ use yii\db\Exception;
* ActiveFinder.php is ...
* todo: lazy loading
* todo: clean up joinOnly and select=false
* todo: records for index != null, asArray = true
* todo: refactor code
* todo: count with
* todo: findBySql and lazy loading cannot apply scopes for primary table
...
...
@@ -84,14 +83,15 @@ class ActiveFinder extends \yii\base\Object
}
$this
->
applyScopes
(
$query
);
$sql
=
$this
->
connection
->
getQueryBuilder
()
->
build
(
$query
);
if
(
strpos
(
$sql
,
'@.'
)
!==
false
)
{
$prefix
=
$this
->
connection
->
quoteTableName
(
'@'
,
true
)
.
'.'
;
if
(
strpos
(
$sql
,
$prefix
)
!==
false
)
{
if
(
$query
->
tableAlias
!==
null
)
{
$alias
=
$this
->
connection
->
quoteTableName
(
$query
->
tableAlias
)
.
'.'
;
}
else
{
$class
=
$query
->
modelClass
;
$alias
=
$this
->
connection
->
quoteTableName
(
$class
::
tableName
())
.
'.'
;
}
$sql
=
str_replace
(
'@.'
,
$alias
,
$sql
);
$sql
=
str_replace
(
$prefix
,
$alias
,
$sql
);
}
}
$command
=
$this
->
connection
->
createCommand
(
$sql
,
$query
->
params
);
...
...
@@ -125,18 +125,31 @@ class ActiveFinder extends \yii\base\Object
return
$records
;
}
/**
* @param ActiveRecord $record
* @param ActiveRelation $relation
* @return array
*/
public
function
findRelatedRecords
(
$record
,
$relation
)
{
$this
->
_joinCount
=
0
;
$this
->
_tableAliases
=
array
();
$this
->
_hasMany
=
false
;
$query
=
new
ActiveQuery
(
get_class
(
$record
));
$modelClass
=
$query
->
modelClass
;
$table
=
$modelClass
::
getMetaData
()
->
table
;
$query
->
select
=
$table
->
primaryKey
;
$query
->
limit
=
$relation
->
limit
;
$query
->
offset
=
$relation
->
offset
;
$joinTree
=
new
JoinElement
(
$this
->
_joinCount
++
,
$query
,
null
,
null
);
$child
=
$this
->
buildJoinTree
(
$joinTree
,
$relation
->
name
);
$child
->
query
=
$relation
;
$child
->
container
=
null
;
$this
->
buildJoinTree
(
$child
,
$relation
->
with
);
$this
->
initJoinTree
(
$joinTree
);
// todo: set where by pk
$pk
=
$record
->
getPrimaryKey
(
true
);
$this
->
addPkCondition
(
$query
,
$table
,
array
(
$pk
),
$query
->
tableAlias
.
'.'
);
$q
=
new
Query
;
$this
->
buildJoinQuery
(
$joinTree
,
$q
);
...
...
@@ -146,16 +159,13 @@ class ActiveFinder extends \yii\base\Object
}
$rows
=
$q
->
createCommand
(
$this
->
connection
)
->
queryAll
();
$
joinTree
->
populateData
(
$rows
);
$
child
->
populateData
(
$rows
);
if
(
$query
->
index
!==
null
)
{
$records
=
array
();
foreach
(
$joinTree
->
records
as
$record
)
{
$records
[
$record
[
$query
->
index
]]
=
$record
;
}
$records
=
$relation
->
index
===
null
?
array_values
(
$child
->
records
)
:
$child
->
records
;
if
(
$relation
->
hasMany
)
{
return
$records
;
}
else
{
return
array_values
(
$joinTree
->
records
);
return
$records
===
array
()
?
null
:
reset
(
$
records
);
}
}
...
...
@@ -563,7 +573,7 @@ class ActiveFinder extends \yii\base\Object
protected
function
addPkCondition
(
$query
,
$table
,
$rows
,
$prefix
)
{
if
(
count
(
$table
->
primaryKey
)
===
1
)
{
if
(
count
(
$table
->
primaryKey
)
===
1
&&
count
(
$rows
)
>
1
)
{
$name
=
$table
->
primaryKey
[
0
];
$values
=
array
();
foreach
(
$rows
as
$row
)
{
...
...
framework/db/ar/ActiveRecord.php
View file @
5ddc1ba1
...
...
@@ -425,7 +425,7 @@ abstract class ActiveRecord extends Model
if
(
isset
(
$md
->
table
->
columns
[
$name
]))
{
return
null
;
}
elseif
(
isset
(
$md
->
relations
[
$name
]))
{
if
(
array_key_exists
(
$name
,
$this
->
_related
))
{
if
(
isset
(
$this
->
_related
[
$name
])
||
$this
->
_related
!==
null
&&
array_key_exists
(
$name
,
$this
->
_related
))
{
return
$this
->
_related
[
$name
];
}
else
{
return
$this
->
_related
[
$name
]
=
$this
->
findByRelation
(
$md
->
relations
[
$name
]);
...
...
@@ -555,6 +555,7 @@ abstract class ActiveRecord extends Model
}
$relation
=
$md
->
relations
[
$relation
];
}
$relation
=
clone
$relation
;
foreach
(
$params
as
$name
=>
$value
)
{
$relation
->
$name
=
$value
;
}
...
...
framework/db/ar/JoinElement.php
View file @
5ddc1ba1
...
...
@@ -57,8 +57,8 @@ class JoinElement extends \yii\base\Object
/**
* @var array query results for this element (PK value => AR instance or data array)
*/
public
$records
;
public
$related
;
public
$records
=
array
()
;
public
$related
=
array
()
;
/**
* @param integer $id
...
...
tests/unit/framework/db/ar/ActiveRecordTest.php
View file @
5ddc1ba1
...
...
@@ -267,45 +267,49 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertTrue
(
is_array
(
$customers
[
1
][
'orders'
][
0
][
'customer'
]));
}
/*
public function testGetSql()
{
// sql for all
$sql = Customer::find()->sql;
$this->assertEquals('SELECT * FROM `tbl_customer`', $sql);
// sql for one row
$sql = Customer::find()->oneSql;
$this->assertEquals('SELECT * FROM tbl_customer LIMIT 1', $sql);
// sql for count
$sql = Customer::find()->countSql;
$this->assertEquals('SELECT COUNT(*) FROM tbl_customer', $sql);
}
public function testArrayResult()
{
Customer::find()->asArray()->one();
Customer::find()->asArray()->all();
}
public function testMisc()
{
// Customer::exists()
// Customer::updateAll()
// Customer::updateCounters()
// Customer::deleteAll()
}
public
function
testLazyLoading
()
{
// has one
$order
=
Order
::
find
(
3
)
->
one
();
$this
->
assertTrue
(
$order
->
customer
instanceof
Customer
);
$this
->
assertEquals
(
2
,
$order
->
customer
->
id
);
}
// has many
$customer
=
Customer
::
find
(
2
)
->
one
();
$orders
=
$customer
->
orders
;
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
id
);
$this
->
assertEquals
(
3
,
$orders
[
1
]
->
id
);
public function testEagerLoading()
{
// has many via join table
$orders
=
Order
::
find
()
->
order
(
'@.id'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
count
(
$orders
[
0
]
->
books
));
$this
->
assertEquals
(
1
,
$orders
[
0
]
->
books
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
books
[
1
]
->
id
);
$this
->
assertEquals
(
array
(),
$orders
[
1
]
->
books
);
$this
->
assertEquals
(
1
,
count
(
$orders
[
2
]
->
books
));
$this
->
assertEquals
(
2
,
$orders
[
2
]
->
books
[
0
]
->
id
);
// customized relation query
$customer
=
Customer
::
find
(
2
)
->
one
();
$orders
=
$customer
->
orders
(
array
(
'where'
=>
'@.id = 3'
,
));
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
$orders
[
0
]
->
id
);
// original results are kept after customized query
$orders
=
$customer
->
orders
;
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
id
);
$this
->
assertEquals
(
3
,
$orders
[
1
]
->
id
);
// as array
$orders
=
$customer
->
orders
(
array
(
'asArray'
=>
true
,
));
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertTrue
(
is_array
(
$orders
[
0
]));
$this
->
assertEquals
(
2
,
$orders
[
0
][
'id'
]);
$this
->
assertEquals
(
3
,
$orders
[
1
][
'id'
]);
}
*/
}
\ No newline at end of file
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