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
37220ee8
Commit
37220ee8
authored
Feb 10, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
9d8a4012
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
163 additions
and
16 deletions
+163
-16
ActiveFinder.php
framework/db/ar/ActiveFinder.php
+7
-1
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+11
-3
Command.php
framework/db/dao/Command.php
+4
-0
QueryBuilder.php
framework/db/dao/QueryBuilder.php
+7
-10
Item.php
tests/unit/data/ar/Item.php
+18
-0
OrderItem.php
tests/unit/data/ar/OrderItem.php
+24
-0
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+92
-2
No files found.
framework/db/ar/ActiveFinder.php
View file @
37220ee8
...
...
@@ -30,6 +30,8 @@ use yii\db\Exception;
* todo: scope
* todo: test via option
*
* @property integer $count
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
...
...
@@ -86,6 +88,10 @@ class ActiveFinder extends \yii\base\Object implements \IteratorAggregate, \Arra
return
$this
->
records
;
}
/**
* @param boolean $limitOne
* @return null|ActiveRecord
*/
public
function
one
(
$limitOne
=
true
)
{
if
(
$this
->
records
===
null
)
{
...
...
@@ -737,7 +743,7 @@ class ActiveFinder extends \yii\base\Object implements \IteratorAggregate, \Arra
$command
=
$this
->
getDbConnection
()
->
createCommand
(
$this
->
sql
);
$command
->
bindValues
(
$this
->
query
->
params
);
}
echo
$command
->
sql
;
$rows
=
$command
->
queryAll
();
if
(
!
empty
(
$this
->
with
))
{
...
...
framework/db/ar/ActiveRecord.php
View file @
37220ee8
...
...
@@ -545,9 +545,17 @@ abstract class ActiveRecord extends Model
}
$names
=
array_flip
(
$names
);
$attributes
=
array
();
foreach
(
$this
->
_attributes
as
$name
=>
$value
)
{
if
(
isset
(
$names
[
$name
])
&&
(
!
array_key_exists
(
$name
,
$this
->
_oldAttributes
)
||
$value
!==
$this
->
_oldAttributes
[
$name
]))
{
$attributes
[
$name
]
=
$value
;
if
(
empty
(
$this
->
_oldAttributes
))
{
foreach
(
$this
->
_attributes
as
$name
=>
$value
)
{
if
(
isset
(
$names
[
$name
]))
{
$attributes
[
$name
]
=
$value
;
}
}
}
else
{
foreach
(
$this
->
_attributes
as
$name
=>
$value
)
{
if
(
isset
(
$names
[
$name
])
&&
(
!
array_key_exists
(
$name
,
$this
->
_oldAttributes
)
||
$value
!==
$this
->
_oldAttributes
[
$name
]))
{
$attributes
[
$name
]
=
$value
;
}
}
}
return
$attributes
;
...
...
framework/db/dao/Command.php
View file @
37220ee8
...
...
@@ -233,6 +233,8 @@ class Command extends \yii\base\Component
$paramLog
=
"
\n
Parameters: "
.
var_export
(
$this
->
_params
,
true
);
}
echo
"Executing SQL:
{
$sql
}{
$paramLog
}
"
.
"
\n\n
"
;
\Yii
::
trace
(
"Executing SQL:
{
$sql
}{
$paramLog
}
"
,
__CLASS__
);
try
{
...
...
@@ -366,6 +368,8 @@ class Command extends \yii\base\Component
$paramLog
=
"
\n
Parameters: "
.
var_export
(
$this
->
_params
,
true
);
}
echo
"Executing SQL:
{
$sql
}{
$paramLog
}
"
.
"
\n\n
"
;
\Yii
::
trace
(
"Querying SQL:
{
$sql
}{
$paramLog
}
"
,
__CLASS__
);
if
(
$db
->
queryCachingCount
>
0
&&
$db
->
queryCachingDuration
>=
0
&&
$method
!==
''
)
{
...
...
framework/db/dao/QueryBuilder.php
View file @
37220ee8
...
...
@@ -94,24 +94,22 @@ class QueryBuilder extends \yii\base\Object
* For example,
*
* ~~~
* $params = array();
* $sql = $queryBuilder->insert('tbl_user', array(
* 'name' => 'Sam',
* 'age' => 30,
* )
, $params
);
* ));
* ~~~
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column data (name=>value) to be inserted into the table.
* @param array $params the parameters to be bound to the query. This method will modify
* this parameter by appending new parameters to be bound to the query.
* @return integer number of rows affected by the execution.
*/
public
function
insert
(
$table
,
$columns
,
&
$params
=
array
()
)
public
function
insert
(
$table
,
$columns
)
{
$names
=
array
();
$placeholders
=
array
();
$count
=
0
;
$params
=
array
();
foreach
(
$columns
as
$name
=>
$value
)
{
$names
[]
=
$this
->
quoteColumnName
(
$name
);
if
(
$value
instanceof
Expression
)
{
...
...
@@ -150,11 +148,10 @@ class QueryBuilder extends \yii\base\Object
* @param array $columns the column data (name=>value) to be updated.
* @param mixed $condition the condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition.
* @param array $params the parameters to be bound to the query. This method will modify
* this parameter by appending new parameters to be bound to the query.
* @param array $params the parameters to be bound to the query.
* @return integer number of rows affected by the execution.
*/
public
function
update
(
$table
,
$columns
,
$condition
=
''
,
&
$params
=
array
())
public
function
update
(
$table
,
$columns
,
$condition
=
''
,
$params
=
array
())
{
$lines
=
array
();
$count
=
0
;
...
...
@@ -528,7 +525,7 @@ class QueryBuilder extends \yii\base\Object
}
}
}
return
'('
.
implode
(
') AND ('
,
$parts
)
.
')'
;
return
count
(
$parts
)
===
1
?
$parts
[
0
]
:
'('
.
implode
(
') AND ('
,
$parts
)
.
')'
;
}
private
function
buildAndCondition
(
$operator
,
$operands
)
...
...
@@ -745,7 +742,7 @@ class QueryBuilder extends \yii\base\Object
}
}
return
implode
(
"
\n
"
,
$joins
);
return
implode
(
$this
->
separator
,
$joins
);
}
/**
...
...
tests/unit/data/ar/Item.php
0 → 100644
View file @
37220ee8
<?php
namespace
yiiunit\data\ar
;
class
Item
extends
ActiveRecord
{
public
static
function
tableName
()
{
return
'tbl_item'
;
}
public
static
function
relations
()
{
return
array
(
);
}
}
\ No newline at end of file
tests/unit/data/ar/OrderItem.php
0 → 100644
View file @
37220ee8
<?php
namespace
yiiunit\data\ar
;
class
OrderItem
extends
ActiveRecord
{
public
static
function
tableName
()
{
return
'tbl_order_item'
;
}
public
static
function
relations
()
{
return
array
(
'order:Order'
=>
array
(
'on'
=>
'@.order_id = ?.id'
,
),
'item:Item'
=>
array
(
'on'
=>
'@.item_id = ?.id'
,
),
);
}
}
\ No newline at end of file
tests/unit/framework/db/ar/ActiveRecordTest.php
View file @
37220ee8
...
...
@@ -3,9 +3,10 @@
namespace
yiiunit\framework\db\ar
;
use
yii\db\dao\Query
;
use
yii\db\ar\Active
Query
;
use
yii\db\ar\Active
Finder
;
use
yiiunit\data\ar\ActiveRecord
;
use
yiiunit\data\ar\Customer
;
use
yiiunit\data\ar\OrderItem
;
class
ActiveRecordTest
extends
\yiiunit\MysqlTestCase
{
...
...
@@ -14,11 +15,100 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
ActiveRecord
::
$db
=
$this
->
getConnection
();
}
public
function
testInsert
()
{
$customer
=
new
Customer
;
$customer
->
email
=
'user4@example.com'
;
$customer
->
name
=
'user4'
;
$customer
->
address
=
'address4'
;
$this
->
assertNull
(
$customer
->
id
);
$this
->
assertTrue
(
$customer
->
isNewRecord
);
$customer
->
save
();
$this
->
assertEquals
(
4
,
$customer
->
id
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
}
public
function
testUpdate
()
{
// save
$customer
=
Customer
::
find
(
2
)
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$customer
->
name
=
'user2x'
;
$customer
->
save
();
$this
->
assertEquals
(
'user2x'
,
$customer
->
name
);
$customer2
=
Customer
::
find
(
2
)
->
one
();
$this
->
assertEquals
(
'user2x'
,
$customer2
->
name
);
// saveAttributes
$customer
=
Customer
::
find
(
1
)
->
one
();
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$this
->
assertEquals
(
'address1'
,
$customer
->
address
);
$customer
->
saveAttributes
(
array
(
'name'
=>
'user1x'
,
'address'
=>
'address1x'
,
));
$this
->
assertEquals
(
'user1x'
,
$customer
->
name
);
$this
->
assertEquals
(
'address1x'
,
$customer
->
address
);
$customer
=
Customer
::
find
(
1
)
->
one
();
$this
->
assertEquals
(
'user1x'
,
$customer
->
name
);
$this
->
assertEquals
(
'address1x'
,
$customer
->
address
);
// saveCounters
$pk
=
array
(
'order_id'
=>
2
,
'item_id'
=>
4
);
$orderItem
=
OrderItem
::
find
(
$pk
)
->
one
();
$this
->
assertEquals
(
1
,
$orderItem
->
quantity
);
$orderItem
->
saveCounters
(
array
(
'quantity'
=>
-
1
));
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
$orderItem
=
OrderItem
::
find
(
$pk
)
->
one
();
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
// updateAll
$customer
=
Customer
::
find
(
3
)
->
one
();
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
Customer
::
updateAll
(
array
(
'name'
=>
'temp'
,
),
array
(
'id'
=>
3
));
$customer
=
Customer
::
find
(
3
)
->
one
();
$this
->
assertEquals
(
'temp'
,
$customer
->
name
);
// updateCounters
$pk
=
array
(
'order_id'
=>
1
,
'item_id'
=>
2
);
$orderItem
=
OrderItem
::
find
(
$pk
)
->
one
();
$this
->
assertEquals
(
2
,
$orderItem
->
quantity
);
OrderItem
::
updateCounters
(
array
(
'quantity'
=>
3
,
),
$pk
);
$orderItem
=
OrderItem
::
find
(
$pk
)
->
one
();
$this
->
assertEquals
(
5
,
$orderItem
->
quantity
);
}
public
function
testDelete
()
{
$post
=
Post2
::
model
()
->
findByPk
(
1
);
$this
->
assertTrue
(
$post
->
delete
());
$this
->
assertNull
(
Post2
::
model
()
->
findByPk
(
1
));
$this
->
assertTrue
(
Post2
::
model
()
->
findByPk
(
2
)
instanceof
Post2
);
$this
->
assertTrue
(
Post2
::
model
()
->
findByPk
(
3
)
instanceof
Post2
);
$this
->
assertEquals
(
2
,
Post2
::
model
()
->
deleteByPk
(
array
(
2
,
3
)));
$this
->
assertNull
(
Post2
::
model
()
->
findByPk
(
2
));
$this
->
assertNull
(
Post2
::
model
()
->
findByPk
(
3
));
$this
->
assertTrue
(
Post2
::
model
()
->
findByPk
(
5
)
instanceof
Post2
);
$this
->
assertEquals
(
1
,
Post2
::
model
()
->
deleteAll
(
'id=5'
));
$this
->
assertNull
(
Post2
::
model
()
->
findByPk
(
5
));
}
public
function
testFind
()
{
// find one
$result
=
Customer
::
find
();
$this
->
assertTrue
(
$result
instanceof
Active
Query
);
$this
->
assertTrue
(
$result
instanceof
Active
Finder
);
$customer
=
$result
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
1
,
$result
->
count
);
...
...
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