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
35688b32
Commit
35688b32
authored
May 13, 2013
by
resurtm
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AR atomic scenarios fixes.
parent
7fa9f113
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
34 deletions
+28
-34
ar.md
docs/internals/ar.md
+2
-8
ActiveRecord.php
yii/db/ActiveRecord.php
+26
-26
No files found.
docs/internals/ar.md
View file @
35688b32
...
@@ -19,19 +19,13 @@ public function scenarios()
...
@@ -19,19 +19,13 @@ public function scenarios()
'atomic'
=>
array
(),
// default value
'atomic'
=>
array
(),
// default value
),
),
// 3. all three operations (insert, update and delete) will be wrapped with transaction
// 3. insert and update operations will be wrapped with transaction, delete won't be wrapped
'scenario3'
=>
array
(
'attributes'
=>
array
(
'attribute1'
,
'attribute2'
),
'atomic'
,
),
// 4. insert and update operations will be wrapped with transaction, delete won't
'scenario4'
=>
array
(
'scenario4'
=>
array
(
'attributes'
=>
array
(
'attribute1'
,
'attribute2'
),
'attributes'
=>
array
(
'attribute1'
,
'attribute2'
),
'atomic'
=>
array
(
self
::
OPERATION_INSERT
,
self
::
OPERATION_UPDATE
),
'atomic'
=>
array
(
self
::
OPERATION_INSERT
,
self
::
OPERATION_UPDATE
),
),
),
// 5. insert and update operations won't be wrapped with transaction, delete will
// 5. insert and update operations won't be wrapped with transaction, delete will
be wrapped
'scenario5'
=>
array
(
'scenario5'
=>
array
(
'attributes'
=>
array
(
'attribute1'
,
'attribute2'
),
'attributes'
=>
array
(
'attribute1'
,
'attribute2'
),
'atomic'
=>
array
(
self
::
OPERATION_DELETE
),
'atomic'
=>
array
(
self
::
OPERATION_DELETE
),
...
...
yii/db/ActiveRecord.php
View file @
35688b32
...
@@ -77,17 +77,17 @@ class ActiveRecord extends Model
...
@@ -77,17 +77,17 @@ class ActiveRecord extends Model
* Represents insert ActiveRecord operation. This constant is used for specifying set of atomic operations
* Represents insert ActiveRecord operation. This constant is used for specifying set of atomic operations
* for particular scenario in the [[scenarios()]] method.
* for particular scenario in the [[scenarios()]] method.
*/
*/
const
OP
ERATION
_INSERT
=
'insert'
;
const
OP_INSERT
=
'insert'
;
/**
/**
* Represents update ActiveRecord operation. This constant is used for specifying set of atomic operations
* Represents update ActiveRecord operation. This constant is used for specifying set of atomic operations
* for particular scenario in the [[scenarios()]] method.
* for particular scenario in the [[scenarios()]] method.
*/
*/
const
OP
ERATION
_UPDATE
=
'update'
;
const
OP_UPDATE
=
'update'
;
/**
/**
* Represents delete ActiveRecord operation. This constant is used for specifying set of atomic operations
* Represents delete ActiveRecord operation. This constant is used for specifying set of atomic operations
* for particular scenario in the [[scenarios()]] method.
* for particular scenario in the [[scenarios()]] method.
*/
*/
const
OP
ERATION
_DELETE
=
'delete'
;
const
OP_DELETE
=
'delete'
;
/**
/**
* @var array attribute values indexed by attribute names
* @var array attribute values indexed by attribute names
...
@@ -688,18 +688,18 @@ class ActiveRecord extends Model
...
@@ -688,18 +688,18 @@ class ActiveRecord extends Model
return
false
;
return
false
;
}
}
$db
=
static
::
getDb
();
$db
=
static
::
getDb
();
$transaction
=
$this
->
isOp
erationAtomic
(
self
::
OPERATION_INSERT
)
&&
null
===
$db
->
getTransaction
()
?
$db
->
beginTransaction
()
:
null
;
$transaction
=
$this
->
isOp
Atomic
(
self
::
OP_INSERT
)
&&
$db
->
getTransaction
()
===
null
?
$db
->
beginTransaction
()
:
null
;
try
{
try
{
$result
=
$this
->
in
ternalInsert
(
$attributes
);
$result
=
$this
->
in
sertInternal
(
$attributes
);
if
(
null
!==
$transaction
)
{
if
(
$transaction
!==
null
)
{
if
(
false
===
$result
)
{
if
(
$result
===
false
)
{
$transaction
->
rollback
();
$transaction
->
rollback
();
}
else
{
}
else
{
$transaction
->
commit
();
$transaction
->
commit
();
}
}
}
}
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
if
(
null
!==
$transaction
)
{
if
(
$transaction
!==
null
)
{
$transaction
->
rollback
();
$transaction
->
rollback
();
}
}
throw
$e
;
throw
$e
;
...
@@ -710,7 +710,7 @@ class ActiveRecord extends Model
...
@@ -710,7 +710,7 @@ class ActiveRecord extends Model
/**
/**
* @see ActiveRecord::insert()
* @see ActiveRecord::insert()
*/
*/
private
function
in
ternalInsert
(
$attributes
=
null
)
private
function
in
sertInternal
(
$attributes
=
null
)
{
{
if
(
!
$this
->
beforeSave
(
true
))
{
if
(
!
$this
->
beforeSave
(
true
))
{
return
false
;
return
false
;
...
@@ -798,18 +798,18 @@ class ActiveRecord extends Model
...
@@ -798,18 +798,18 @@ class ActiveRecord extends Model
return
false
;
return
false
;
}
}
$db
=
static
::
getDb
();
$db
=
static
::
getDb
();
$transaction
=
$this
->
isOp
erationAtomic
(
self
::
OPERATION_UPDATE
)
&&
null
===
$db
->
getTransaction
()
?
$db
->
beginTransaction
()
:
null
;
$transaction
=
$this
->
isOp
Atomic
(
self
::
OP_UPDATE
)
&&
$db
->
getTransaction
()
===
null
?
$db
->
beginTransaction
()
:
null
;
try
{
try
{
$result
=
$this
->
internalUpdate
(
$attributes
);
$result
=
$this
->
updateInternal
(
$attributes
);
if
(
null
!==
$transaction
)
{
if
(
$transaction
!==
null
)
{
if
(
false
===
$result
)
{
if
(
$result
===
false
)
{
$transaction
->
rollback
();
$transaction
->
rollback
();
}
else
{
}
else
{
$transaction
->
commit
();
$transaction
->
commit
();
}
}
}
}
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
if
(
null
!==
$transaction
)
{
if
(
$transaction
!==
null
)
{
$transaction
->
rollback
();
$transaction
->
rollback
();
}
}
throw
$e
;
throw
$e
;
...
@@ -821,7 +821,7 @@ class ActiveRecord extends Model
...
@@ -821,7 +821,7 @@ class ActiveRecord extends Model
* @see CActiveRecord::update()
* @see CActiveRecord::update()
* @throws StaleObjectException
* @throws StaleObjectException
*/
*/
private
function
internalUpdate
(
$attributes
=
null
)
private
function
updateInternal
(
$attributes
=
null
)
{
{
if
(
!
$this
->
beforeSave
(
false
))
{
if
(
!
$this
->
beforeSave
(
false
))
{
return
false
;
return
false
;
...
@@ -905,7 +905,7 @@ class ActiveRecord extends Model
...
@@ -905,7 +905,7 @@ class ActiveRecord extends Model
public
function
delete
()
public
function
delete
()
{
{
$db
=
static
::
getDb
();
$db
=
static
::
getDb
();
$transaction
=
$this
->
isOp
erationAtomic
(
self
::
OPERATION_DELETE
)
&&
null
===
$db
->
getTransaction
()
?
$db
->
beginTransaction
()
:
null
;
$transaction
=
$this
->
isOp
Atomic
(
self
::
OP_DELETE
)
&&
$db
->
getTransaction
()
===
null
?
$db
->
beginTransaction
()
:
null
;
try
{
try
{
$result
=
false
;
$result
=
false
;
if
(
$this
->
beforeDelete
())
{
if
(
$this
->
beforeDelete
())
{
...
@@ -913,25 +913,25 @@ class ActiveRecord extends Model
...
@@ -913,25 +913,25 @@ class ActiveRecord extends Model
// the record is already deleted in the database and thus the method will return 0
// the record is already deleted in the database and thus the method will return 0
$condition
=
$this
->
getOldPrimaryKey
(
true
);
$condition
=
$this
->
getOldPrimaryKey
(
true
);
$lock
=
$this
->
optimisticLock
();
$lock
=
$this
->
optimisticLock
();
if
(
null
!==
$lock
)
{
if
(
$lock
!==
null
)
{
$condition
[
$lock
]
=
$this
->
$lock
;
$condition
[
$lock
]
=
$this
->
$lock
;
}
}
$result
=
$this
->
deleteAll
(
$condition
);
$result
=
$this
->
deleteAll
(
$condition
);
if
(
null
!==
$lock
&&
!
$result
)
{
if
(
$lock
!==
null
&&
!
$result
)
{
throw
new
StaleObjectException
(
'The object being deleted is outdated.'
);
throw
new
StaleObjectException
(
'The object being deleted is outdated.'
);
}
}
$this
->
_oldAttributes
=
null
;
$this
->
_oldAttributes
=
null
;
$this
->
afterDelete
();
$this
->
afterDelete
();
}
}
if
(
null
!==
$transaction
)
{
if
(
$transaction
!==
null
)
{
if
(
false
===
$result
)
{
if
(
$result
===
false
)
{
$transaction
->
rollback
();
$transaction
->
rollback
();
}
else
{
}
else
{
$transaction
->
commit
();
$transaction
->
commit
();
}
}
}
}
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
if
(
null
!==
$transaction
)
{
if
(
$transaction
!==
null
)
{
$transaction
->
rollback
();
$transaction
->
rollback
();
}
}
throw
$e
;
throw
$e
;
...
@@ -1428,17 +1428,17 @@ class ActiveRecord extends Model
...
@@ -1428,17 +1428,17 @@ class ActiveRecord extends Model
}
}
/**
/**
* @param string $op
eration
possible values are ActiveRecord::INSERT, ActiveRecord::UPDATE and ActiveRecord::DELETE.
* @param string $op possible values are ActiveRecord::INSERT, ActiveRecord::UPDATE and ActiveRecord::DELETE.
* @return boolean whether given operation is atomic. Currently active scenario is taken into account.
* @return boolean whether given operation is atomic. Currently active scenario is taken into account.
*/
*/
private
function
isOp
erationAtomic
(
$operation
)
private
function
isOp
Atomic
(
$op
)
{
{
$scenario
=
$this
->
getScenario
();
$scenario
=
$this
->
getScenario
();
$scenarios
=
$this
->
scenarios
();
$scenarios
=
$this
->
scenarios
();
if
(
!
isset
(
$scenarios
[
$scenario
])
||
!
isset
(
$scenarios
[
$scenario
][
'attributes'
])
||
!
is_array
(
$scenarios
[
$scenario
][
'attributes'
]))
{
if
(
isset
(
$scenarios
[
$scenario
],
$scenario
[
$scenario
][
'atomic'
])
&&
is_array
(
$scenarios
[
$scenario
][
'atomic'
]))
{
return
in_array
(
$op
,
$scenarios
[
$scenario
][
'atomic'
]);
}
else
{
return
false
;
return
false
;
}
}
return
in_array
(
'atomic'
,
$scenarios
[
$scenario
])
||
isset
(
$scenarios
[
$scenario
][
'atomic'
])
&&
is_array
(
$scenarios
[
$scenario
][
'atomic'
])
&&
in_array
(
$operation
,
$scenarios
[
$scenario
][
'atomic'
]);
}
}
}
}
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