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
1c93c503
Commit
1c93c503
authored
May 07, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #160 from creocoder/active-record-refactoring
ActiveRecord::insert() and ActiveRecord::update() refactoring
parents
dd596914
e3f6faf5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
49 deletions
+42
-49
ActiveRecord.php
framework/db/ActiveRecord.php
+42
-49
No files found.
framework/db/ActiveRecord.php
View file @
1c93c503
...
@@ -667,36 +667,33 @@ class ActiveRecord extends Model
...
@@ -667,36 +667,33 @@ class ActiveRecord extends Model
*/
*/
public
function
insert
(
$runValidation
=
true
,
$attributes
=
null
)
public
function
insert
(
$runValidation
=
true
,
$attributes
=
null
)
{
{
if
(
$runValidation
&&
!
$this
->
validate
(
$attributes
))
{
if
(
$runValidation
&&
!
$this
->
validate
(
$attributes
)
||
!
$this
->
beforeSave
(
true
)
)
{
return
false
;
return
false
;
}
}
if
(
$this
->
beforeSave
(
true
))
{
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
if
(
empty
(
$values
))
{
if
(
$values
===
array
())
{
foreach
(
$this
->
primaryKey
()
as
$key
)
{
foreach
(
$this
->
primaryKey
()
as
$key
)
{
$values
[
$key
]
=
isset
(
$this
->
_attributes
[
$key
])
?
$this
->
_attributes
[
$key
]
:
null
;
$values
[
$key
]
=
isset
(
$this
->
_attributes
[
$key
])
?
$this
->
_attributes
[
$key
]
:
null
;
}
}
}
$db
=
static
::
getDb
();
}
$command
=
$db
->
createCommand
()
->
insert
(
$this
->
tableName
(),
$values
);
$db
=
static
::
getDb
(
);
if
(
$command
->
execute
())
{
$command
=
$db
->
createCommand
()
->
insert
(
$this
->
tableName
(),
$values
);
$table
=
$this
->
getTableSchema
();
if
(
$command
->
execute
())
{
if
(
$table
->
sequenceName
!==
null
)
{
$table
=
$this
->
getTableSchema
();
foreach
(
$table
->
primaryKey
as
$name
)
{
if
(
$table
->
sequenceName
!==
null
)
{
if
(
!
isset
(
$this
->
_attributes
[
$name
])
)
{
foreach
(
$table
->
primaryKey
as
$name
)
{
$this
->
_oldAttributes
[
$name
]
=
$this
->
_attributes
[
$name
]
=
$db
->
getLastInsertID
(
$table
->
sequenceName
);
if
(
!
isset
(
$this
->
_attributes
[
$name
]))
{
break
;
$this
->
_oldAttributes
[
$name
]
=
$this
->
_attributes
[
$name
]
=
$db
->
getLastInsertID
(
$table
->
sequenceName
)
;
}
break
;
}
}
}
}
foreach
(
$values
as
$name
=>
$value
)
{
$this
->
_oldAttributes
[
$name
]
=
$value
;
}
$this
->
afterSave
(
true
);
return
true
;
}
}
foreach
(
$values
as
$name
=>
$value
)
{
$this
->
_oldAttributes
[
$name
]
=
$value
;
}
$this
->
afterSave
(
true
);
return
true
;
}
}
return
false
;
}
}
/**
/**
...
@@ -750,39 +747,35 @@ class ActiveRecord extends Model
...
@@ -750,39 +747,35 @@ class ActiveRecord extends Model
*/
*/
public
function
update
(
$runValidation
=
true
,
$attributes
=
null
)
public
function
update
(
$runValidation
=
true
,
$attributes
=
null
)
{
{
if
(
$runValidation
&&
!
$this
->
validate
(
$attributes
))
{
if
(
$runValidation
&&
!
$this
->
validate
(
$attributes
)
||
!
$this
->
beforeSave
(
false
)
)
{
return
false
;
return
false
;
}
}
if
(
$this
->
beforeSave
(
false
))
{
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
if
(
!
empty
(
$values
))
{
if
(
$values
!==
array
())
{
$condition
=
$this
->
getOldPrimaryKey
(
true
);
$condition
=
$this
->
getOldPrimaryKey
(
true
);
$lock
=
$this
->
optimisticLock
();
$lock
=
$this
->
optimisticLock
();
if
(
$lock
!==
null
)
{
if
(
$lock
!==
null
)
{
if
(
!
isset
(
$values
[
$lock
]))
{
if
(
!
isset
(
$values
[
$lock
]))
{
$values
[
$lock
]
=
$this
->
$lock
+
1
;
$values
[
$lock
]
=
$this
->
$lock
+
1
;
}
$condition
[
$lock
]
=
$this
->
$lock
;
}
// 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.
$rows
=
$this
->
updateAll
(
$values
,
$condition
);
if
(
$lock
!==
null
&&
!
$rows
)
{
throw
new
StaleObjectException
(
'The object being updated is outdated.'
);
}
}
$condition
[
$lock
]
=
$this
->
$lock
;
}
// 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.
$rows
=
$this
->
updateAll
(
$values
,
$condition
);
foreach
(
$values
as
$name
=>
$value
)
{
if
(
$lock
!==
null
&&
!
$rows
)
{
$this
->
_oldAttributes
[
$name
]
=
$this
->
_attributes
[
$name
]
;
throw
new
StaleObjectException
(
'The object being updated is outdated.'
)
;
}
}
$this
->
afterSave
(
false
);
foreach
(
$values
as
$name
=>
$value
)
{
return
$rows
;
$this
->
_oldAttributes
[
$name
]
=
$this
->
_attributes
[
$name
];
}
else
{
return
0
;
}
}
$this
->
afterSave
(
false
);
return
$rows
;
}
else
{
}
else
{
return
false
;
return
0
;
}
}
}
}
...
...
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