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
906e402b
Commit
906e402b
authored
Mar 31, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ActiveRecord::update() and ActiveRecord::delete() now returns the number rows affected.
parent
c32a202d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
9 deletions
+39
-9
ActiveRecord.php
framework/db/ActiveRecord.php
+29
-9
QueryBuilder.php
framework/db/QueryBuilder.php
+10
-0
No files found.
framework/db/ActiveRecord.php
View file @
906e402b
...
...
@@ -530,8 +530,8 @@ class ActiveRecord extends Model
*/
public
function
isAttributeChanged
(
$name
)
{
if
(
isset
(
$this
->
_attribute
[
$name
],
$this
->
_oldAttributes
[
$name
]))
{
return
$this
->
_attribute
[
$name
]
!==
$this
->
_oldAttributes
[
$name
];
if
(
isset
(
$this
->
_attribute
s
[
$name
],
$this
->
_oldAttributes
[
$name
]))
{
return
$this
->
_attribute
s
[
$name
]
!==
$this
->
_oldAttributes
[
$name
];
}
else
{
return
isset
(
$this
->
_attributes
[
$name
])
||
isset
(
$this
->
_oldAttributes
);
}
...
...
@@ -590,7 +590,11 @@ class ActiveRecord extends Model
*/
public
function
save
(
$runValidation
=
true
,
$attributes
=
null
)
{
return
$this
->
getIsNewRecord
()
?
$this
->
insert
(
$runValidation
,
$attributes
)
:
$this
->
update
(
$runValidation
,
$attributes
);
if
(
$this
->
getIsNewRecord
())
{
return
$this
->
insert
(
$runValidation
,
$attributes
);
}
else
{
return
$this
->
update
(
$runValidation
,
$attributes
)
!==
false
;
}
}
/**
...
...
@@ -692,11 +696,24 @@ class ActiveRecord extends Model
* $customer->update();
* ~~~
*
* Note that it is possible the update does not affect any row in the table.
* In this case, this method will return 0. For this reason, you should use the following
* code to check if update() is successful or not:
*
* ~~~
* if ($this->update() !== false) {
* // update successful
* } else {
* // update failed
* }
* ~~~
*
* @param boolean $runValidation whether to perform validation before saving the record.
* If the validation fails, the record will not be inserted into the database.
* @param array $attributes list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @return boolean whether the attributes are valid and the record is updated successfully.
* @return integer|boolean the number of rows affected, or false if validation fails
* or [[beforeSave()]] stops the updating process.
*/
public
function
update
(
$runValidation
=
true
,
$attributes
=
null
)
{
...
...
@@ -708,13 +725,15 @@ class ActiveRecord extends Model
if
(
$values
!==
array
())
{
// We do not check the return value of updateAll() because it's possible
// that the UPDATE statement doesn't change anything and thus returns 0.
$this
->
updateAll
(
$values
,
$this
->
getOldPrimaryKey
(
true
));
$
rows
=
$
this
->
updateAll
(
$values
,
$this
->
getOldPrimaryKey
(
true
));
foreach
(
$values
as
$name
=>
$value
)
{
$this
->
_oldAttributes
[
$name
]
=
$this
->
_attributes
[
$name
];
}
$this
->
afterSave
(
false
);
return
$rows
;
}
else
{
return
0
;
}
return
true
;
}
else
{
return
false
;
}
...
...
@@ -763,17 +782,18 @@ class ActiveRecord extends Model
* In the above step 1 and 3, events named [[EVENT_BEFORE_DELETE]] and [[EVENT_AFTER_DELETE]]
* will be raised by the corresponding methods.
*
* @return boolean whether the deletion is successful.
* @return integer|boolean the number of rows deleted, or false if the deletion is unsuccessful for some reason.
* Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
*/
public
function
delete
()
{
if
(
$this
->
beforeDelete
())
{
// we do not check the return value of deleteAll() because it's possible
// the record is already deleted in the database and thus the method will return 0
$this
->
deleteAll
(
$this
->
getPrimaryKey
(
true
));
$
rows
=
$
this
->
deleteAll
(
$this
->
getPrimaryKey
(
true
));
$this
->
_oldAttributes
=
null
;
$this
->
afterDelete
();
return
true
;
return
$rows
;
}
else
{
return
false
;
}
...
...
framework/db/QueryBuilder.php
View file @
906e402b
...
...
@@ -751,6 +751,11 @@ class QueryBuilder extends \yii\base\Object
}
if
(
$value
===
null
)
{
$parts
[]
=
"
$column
IS NULL"
;
}
elseif
(
$value
instanceof
Expression
)
{
$parts
[]
=
"
$column
="
.
$value
->
expression
;
foreach
(
$value
->
params
as
$n
=>
$v
)
{
$params
[
$n
]
=
$v
;
}
}
else
{
$phName
=
self
::
PARAM_PREFIX
.
count
(
$params
);
$parts
[]
=
"
$column
=
$phName
"
;
...
...
@@ -823,6 +828,11 @@ class QueryBuilder extends \yii\base\Object
}
if
(
$value
===
null
)
{
$values
[
$i
]
=
'NULL'
;
}
elseif
(
$value
instanceof
Expression
)
{
$values
[
$i
]
=
$value
->
expression
;
foreach
(
$value
->
params
as
$n
=>
$v
)
{
$params
[
$n
]
=
$v
;
}
}
else
{
$phName
=
self
::
PARAM_PREFIX
.
count
(
$params
);
$params
[
$phName
]
=
$value
;
...
...
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