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
cf10943c
Commit
cf10943c
authored
Feb 10, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
7f467291
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
60 additions
and
75 deletions
+60
-75
ActiveFinder.php
framework/db/ar/ActiveFinder.php
+36
-54
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+11
-15
Query.php
framework/db/dao/Query.php
+0
-0
QueryBuilder.php
framework/db/dao/QueryBuilder.php
+5
-2
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+8
-4
No files found.
framework/db/ar/ActiveFinder.php
View file @
cf10943c
...
...
@@ -17,7 +17,8 @@ use yii\db\Exception;
/**
* ActiveFinder.php is ...
* todo: add SQL monitor
*
* todo: better handling on join() support in QueryBuilder: use regexp to detect table name and quote it
* todo: do not support anonymous parameter binding
* todo: add ActiveFinderBuilder
* todo: quote join/on part of the relational query
* todo: modify QueryBuilder about join() methods
...
...
@@ -29,6 +30,7 @@ use yii\db\Exception;
* todo: lazy loading
* todo: scope
* todo: test via option
* todo: count, sum, exists
*
* @property integer $count
*
...
...
@@ -80,6 +82,10 @@ class ActiveFinder extends \yii\base\Object implements \IteratorAggregate, \Arra
$this
->
query
=
new
Query
;
}
/**
* Executes query and returns all results as an array.
* @return array the query results. If the query results in nothing, an empty array will be returned.
*/
public
function
all
()
{
if
(
$this
->
records
===
null
)
{
...
...
@@ -89,15 +95,14 @@ class ActiveFinder extends \yii\base\Object implements \IteratorAggregate, \Arra
}
/**
* @param boolean $limitOne
* @return null|ActiveRecord
* Executes query and returns a single row of result.
* @return null|array|ActiveRecord the single row of query result. Depending on the setting of [[asArray]],
* the query result may be either an array or an ActiveRecord object. Null will be returned
* if the query results in nothing.
*/
public
function
one
(
$limitOne
=
true
)
public
function
one
()
{
if
(
$this
->
records
===
null
)
{
if
(
$limitOne
)
{
$this
->
limit
(
1
);
}
$this
->
records
=
$this
->
findRecords
();
}
return
isset
(
$this
->
records
[
0
])
?
$this
->
records
[
0
]
:
null
;
...
...
@@ -442,96 +447,73 @@ class ActiveFinder extends \yii\base\Object implements \IteratorAggregate, \Arra
}
/**
* Appends an INNER JOIN part to the query.
* Appends a JOIN part to the query.
* The first parameter specifies what type of join it is.
* @param string $type the type of join, such as INNER JOIN, LEFT JOIN.
* @param string $table the table to be joined.
* Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
* The method will automatically quote the table name unless it contains some parenthesis
* (which means the table is given as a sub-query or DB expression).
* @param string|array $
conditi
on the join condition that should appear in the ON part.
* @param string|array $on the join condition that should appear in the ON part.
* Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return ActiveFinder the query object itself
* @return Query the query object itself
*/
public
function
join
(
$t
able
,
$condition
,
$params
=
array
())
public
function
join
(
$t
ype
,
$table
,
$on
=
''
,
$params
=
array
())
{
if
(
is_array
(
$params
))
{
$this
->
query
->
join
(
$table
,
$condition
,
$params
);
}
else
{
call_user_func_array
(
array
(
$this
->
query
,
__FUNCTION__
),
func_get_args
());
}
$this
->
query
->
join
(
$type
,
$table
,
$on
,
$params
);
return
$this
;
}
/**
* Appends a
LEFT OUT
ER JOIN part to the query.
* Appends a
n INN
ER JOIN part to the query.
* @param string $table the table to be joined.
* Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
* The method will automatically quote the table name unless it contains some parenthesis
* (which means the table is given as a sub-query or DB expression).
* @param string|array $
conditi
on the join condition that should appear in the ON part.
* @param string|array $on the join condition that should appear in the ON part.
* Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query
* @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return ActiveFinder the query object itself
*/
public
function
leftJoin
(
$table
,
$conditi
on
,
$params
=
array
())
public
function
innerJoin
(
$table
,
$
on
,
$params
=
array
())
{
if
(
is_array
(
$params
))
{
$this
->
query
->
leftJoin
(
$table
,
$condition
,
$params
);
}
else
{
call_user_func_array
(
array
(
$this
->
query
,
__FUNCTION__
),
func_get_args
());
}
$this
->
query
->
join
(
$table
,
$on
,
$params
);
return
$this
;
}
/**
* Appends a
RIGH
T OUTER JOIN part to the query.
* Appends a
LEF
T OUTER JOIN part to the query.
* @param string $table the table to be joined.
* Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
* The method will automatically quote the table name unless it contains some parenthesis
* (which means the table is given as a sub-query or DB expression).
* @param string|array $
conditi
on the join condition that should appear in the ON part.
* @param string|array $on the join condition that should appear in the ON part.
* Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query
* @return ActiveFinder the query object itself
*/
public
function
rightJoin
(
$table
,
$condition
,
$params
=
array
())
{
if
(
is_array
(
$params
))
{
$this
->
query
->
rightJoin
(
$table
,
$condition
,
$params
);
}
else
{
call_user_func_array
(
array
(
$this
->
query
,
__FUNCTION__
),
func_get_args
());
}
return
$this
;
}
/**
* Appends a CROSS JOIN part to the query.
* Note that not all DBMS support CROSS JOIN.
* @param string $table the table to be joined.
* Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
* The method will automatically quote the table name unless it contains some parenthesis
* (which means the table is given as a sub-query or DB expression).
* @return ActiveFinder the query object itself
*/
public
function
crossJoin
(
$table
)
public
function
leftJoin
(
$table
,
$on
,
$params
=
array
())
{
$this
->
query
->
crossJoin
(
$table
);
$this
->
query
->
leftJoin
(
$table
,
$on
,
$params
);
return
$this
;
}
/**
* Appends a NATURAL JOIN part to the query.
* Note that not all DBMS support NATURAL JOIN.
* Appends a RIGHT OUTER JOIN part to the query.
* @param string $table the table to be joined.
* Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
* The method will automatically quote the table name unless it contains some parenthesis
* (which means the table is given as a sub-query or DB expression).
* @param string|array $on the join condition that should appear in the ON part.
* Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query
* @return ActiveFinder the query object itself
*/
public
function
naturalJoin
(
$table
)
public
function
rightJoin
(
$table
,
$on
,
$params
=
array
()
)
{
$this
->
query
->
naturalJoin
(
$table
);
$this
->
query
->
rightJoin
(
$table
,
$on
,
$params
);
return
$this
;
}
...
...
@@ -746,7 +728,7 @@ class ActiveFinder extends \yii\base\Object implements \IteratorAggregate, \Arra
$rows
=
$command
->
queryAll
();
if
(
!
empty
(
$this
->
with
))
{
if
(
isset
(
$joinTree
))
{
foreach
(
$rows
as
$row
)
{
$joinTree
->
populateData
(
$row
);
}
...
...
framework/db/ar/ActiveRecord.php
View file @
cf10943c
...
...
@@ -50,6 +50,17 @@ abstract class ActiveRecord extends Model
}
/**
* Returns the database connection used by this AR class.
* By default, the "db" application component is used as the database connection.
* You may override this method if you want to use a different database connection.
* @return Connection the database connection used by this AR class.
*/
public
static
function
getDbConnection
()
{
return
\Yii
::
$application
->
getDb
();
}
/**
* Creates an [[ActiveFinder]] instance for query purpose.
*
* Because [[ActiveFinder]] implements a set of query building methods,
...
...
@@ -105,10 +116,6 @@ abstract class ActiveRecord extends Model
*/
public
static
function
findBySql
(
$sql
,
$params
=
array
())
{
if
(
!
is_array
(
$params
))
{
$params
=
func_get_args
();
unset
(
$params
[
0
]);
}
$finder
=
static
::
createActiveFinder
();
$finder
->
sql
=
$sql
;
return
$finder
->
params
(
$params
);
...
...
@@ -155,17 +162,6 @@ abstract class ActiveRecord extends Model
}
/**
* Returns the database connection used by this AR class.
* By default, the "db" application component is used as the database connection.
* You may override this method if you want to use a different database connection.
* @return Connection the database connection used by this AR class.
*/
public
static
function
getDbConnection
()
{
return
\Yii
::
$application
->
getDb
();
}
/**
* Declares the name of the database table associated with this AR class.
* By default this method returns the class name as the table name by calling [[Text::camel2id()]].
* For example, 'Customer' becomes 'customer', and 'OrderDetail' becomes 'order_detail'.
...
...
framework/db/dao/Query.php
View file @
cf10943c
This diff is collapsed.
Click to expand it.
framework/db/dao/QueryBuilder.php
View file @
cf10943c
...
...
@@ -488,7 +488,7 @@ class QueryBuilder extends \yii\base\Object
);
if
(
!
is_array
(
$condition
))
{
return
$condition
;
return
(
string
)
$condition
;
}
elseif
(
$condition
===
array
())
{
return
''
;
}
...
...
@@ -733,9 +733,12 @@ class QueryBuilder extends \yii\base\Object
}
}
$joins
[
$i
]
=
strtoupper
(
$join
[
0
])
.
' '
.
$table
;
if
(
isset
(
$join
[
2
]))
{
// join condition
if
(
isset
(
$join
[
2
]))
{
$condition
=
$this
->
buildCondition
(
$join
[
2
]);
if
(
$condition
!==
''
)
{
$joins
[
$i
]
.=
' ON '
.
$this
->
buildCondition
(
$join
[
2
]);
}
}
}
else
{
throw
new
Exception
(
'A join clause must be specified as an array of at least two elements.'
);
}
...
...
tests/unit/framework/db/ar/ActiveRecordTest.php
View file @
cf10943c
...
...
@@ -63,7 +63,8 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$pk
=
array
(
'order_id'
=>
2
,
'item_id'
=>
4
);
$orderItem
=
OrderItem
::
find
(
$pk
)
->
one
();
$this
->
assertEquals
(
1
,
$orderItem
->
quantity
);
$orderItem
->
saveCounters
(
array
(
'quantity'
=>
-
1
));
$ret
=
$orderItem
->
saveCounters
(
array
(
'quantity'
=>
-
1
));
$this
->
assertTrue
(
$ret
);
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
$orderItem
=
OrderItem
::
find
(
$pk
)
->
one
();
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
...
...
@@ -71,9 +72,10 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
// updateAll
$customer
=
Customer
::
find
(
3
)
->
one
();
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
Customer
::
updateAll
(
array
(
$ret
=
Customer
::
updateAll
(
array
(
'name'
=>
'temp'
,
),
array
(
'id'
=>
3
));
$this
->
assertEquals
(
1
,
$ret
);
$customer
=
Customer
::
find
(
3
)
->
one
();
$this
->
assertEquals
(
'temp'
,
$customer
->
name
);
...
...
@@ -81,9 +83,10 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$pk
=
array
(
'order_id'
=>
1
,
'item_id'
=>
2
);
$orderItem
=
OrderItem
::
find
(
$pk
)
->
one
();
$this
->
assertEquals
(
2
,
$orderItem
->
quantity
);
OrderItem
::
updateCounters
(
array
(
$ret
=
OrderItem
::
updateCounters
(
array
(
'quantity'
=>
3
,
),
$pk
);
$this
->
assertEquals
(
1
,
$ret
);
$orderItem
=
OrderItem
::
find
(
$pk
)
->
one
();
$this
->
assertEquals
(
5
,
$orderItem
->
quantity
);
}
...
...
@@ -101,7 +104,8 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
// deleteAll
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
Customer
::
deleteAll
();
$ret
=
Customer
::
deleteAll
();
$this
->
assertEquals
(
2
,
$ret
);
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
}
...
...
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