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
b94400ba
Commit
b94400ba
authored
May 13, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #253 from resurtm/ar-transactions-3
Fixes #226: atomic operations and transaction support in AR.
parents
31a46f5b
4973838b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
159 additions
and
26 deletions
+159
-26
ActiveRecord.md
docs/api/db/ActiveRecord.md
+4
-0
ar.md
docs/internals/ar.md
+21
-4
Model.php
yii/base/Model.php
+16
-9
ActiveRecord.php
yii/db/ActiveRecord.php
+118
-13
No files found.
docs/api/db/ActiveRecord.md
View file @
b94400ba
...
@@ -446,3 +446,7 @@ $customers = Customer::find()->olderThan(50)->all();
...
@@ -446,3 +446,7 @@ $customers = Customer::find()->olderThan(50)->all();
The parameters should follow after the
`$query`
parameter when defining the scope method, and they
The parameters should follow after the
`$query`
parameter when defining the scope method, and they
can take default values like shown above.
can take default values like shown above.
### Atomic operations and scenarios
TBD
docs/internals/ar.md
View file @
b94400ba
ActiveRecord
ActiveRecord
============
============
Scenarios
---------
All possible scenario formats supported by ActiveRecord:
```
php
public
function
scenarios
()
{
return
array
(
// attributes array, all operations won't be wrapped with transaction
'scenario1'
=>
array
(
'attribute1'
,
'attribute2'
),
// insert and update operations will be wrapped with transaction, delete won't be wrapped
'scenario2'
=>
array
(
'attributes'
=>
array
(
'attribute1'
,
'attribute2'
),
'atomic'
=>
array
(
self
::
OP_INSERT
,
self
::
OP_UPDATE
),
),
);
}
```
Query
Query
-----
-----
### Basic Queries
### Basic Queries
### Relational Queries
### Relational Queries
### Scopes
### Scopes
yii/base/Model.php
View file @
b94400ba
...
@@ -590,42 +590,49 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -590,42 +590,49 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
/**
/**
* Returns the attribute names that are safe to be massively assigned in the current scenario.
* Returns the attribute names that are safe to be massively assigned in the current scenario.
* @return
array
safe attribute names
* @return
string[]
safe attribute names
*/
*/
public
function
safeAttributes
()
public
function
safeAttributes
()
{
{
$scenario
=
$this
->
getScenario
();
$scenario
=
$this
->
getScenario
();
$scenarios
=
$this
->
scenarios
();
$scenarios
=
$this
->
scenarios
();
if
(
!
isset
(
$scenarios
[
$scenario
]))
{
return
array
();
}
$attributes
=
array
();
$attributes
=
array
();
if
(
isset
(
$scenarios
[
$scenario
]))
{
if
(
isset
(
$scenarios
[
$scenario
][
'attributes'
])
&&
is_array
(
$scenarios
[
$scenario
][
'attributes'
]))
{
$scenarios
[
$scenario
]
=
$scenarios
[
$scenario
][
'attributes'
];
}
foreach
(
$scenarios
[
$scenario
]
as
$attribute
)
{
foreach
(
$scenarios
[
$scenario
]
as
$attribute
)
{
if
(
$attribute
[
0
]
!==
'!'
)
{
if
(
$attribute
[
0
]
!==
'!'
)
{
$attributes
[]
=
$attribute
;
$attributes
[]
=
$attribute
;
}
}
}
}
}
return
$attributes
;
return
$attributes
;
}
}
/**
/**
* Returns the attribute names that are subject to validation in the current scenario.
* Returns the attribute names that are subject to validation in the current scenario.
* @return
array
safe attribute names
* @return
string[]
safe attribute names
*/
*/
public
function
activeAttributes
()
public
function
activeAttributes
()
{
{
$scenario
=
$this
->
getScenario
();
$scenario
=
$this
->
getScenario
();
$scenarios
=
$this
->
scenarios
();
$scenarios
=
$this
->
scenarios
();
if
(
isset
(
$scenarios
[
$scenario
]))
{
if
(
!
isset
(
$scenarios
[
$scenario
]))
{
$attributes
=
$scenarios
[
$this
->
getScenario
()];
return
array
();
}
if
(
isset
(
$scenarios
[
$scenario
][
'attributes'
])
&&
is_array
(
$scenarios
[
$scenario
][
'attributes'
]))
{
$attributes
=
$scenarios
[
$scenario
][
'attributes'
];
}
else
{
$attributes
=
$scenarios
[
$scenario
];
}
foreach
(
$attributes
as
$i
=>
$attribute
)
{
foreach
(
$attributes
as
$i
=>
$attribute
)
{
if
(
$attribute
[
0
]
===
'!'
)
{
if
(
$attribute
[
0
]
===
'!'
)
{
$attributes
[
$i
]
=
substr
(
$attribute
,
1
);
$attributes
[
$i
]
=
substr
(
$attribute
,
1
);
}
}
}
}
return
$attributes
;
return
$attributes
;
}
else
{
return
array
();
}
}
}
/**
/**
...
...
yii/db/ActiveRecord.php
View file @
b94400ba
...
@@ -74,6 +74,22 @@ class ActiveRecord extends Model
...
@@ -74,6 +74,22 @@ class ActiveRecord extends Model
const
EVENT_AFTER_DELETE
=
'afterDelete'
;
const
EVENT_AFTER_DELETE
=
'afterDelete'
;
/**
/**
* Represents insert ActiveRecord operation. This constant is used for specifying set of atomic operations
* for particular scenario in the [[scenarios()]] method.
*/
const
OP_INSERT
=
'insert'
;
/**
* Represents update ActiveRecord operation. This constant is used for specifying set of atomic operations
* for particular scenario in the [[scenarios()]] method.
*/
const
OP_UPDATE
=
'update'
;
/**
* Represents delete ActiveRecord operation. This constant is used for specifying set of atomic operations
* for particular scenario in the [[scenarios()]] method.
*/
const
OP_DELETE
=
'delete'
;
/**
* @var array attribute values indexed by attribute names
* @var array attribute values indexed by attribute names
*/
*/
private
$_attributes
=
array
();
private
$_attributes
=
array
();
...
@@ -664,10 +680,39 @@ class ActiveRecord extends Model
...
@@ -664,10 +680,39 @@ class ActiveRecord extends Model
* @param array $attributes list of attributes that need to be saved. Defaults to null,
* @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.
* meaning all attributes that are loaded from DB will be saved.
* @return boolean whether the attributes are valid and the record is inserted successfully.
* @return boolean whether the attributes are valid and the record is inserted successfully.
* @throws \Exception in case insert failed.
*/
*/
public
function
insert
(
$runValidation
=
true
,
$attributes
=
null
)
public
function
insert
(
$runValidation
=
true
,
$attributes
=
null
)
{
{
if
(
$runValidation
&&
!
$this
->
validate
(
$attributes
)
||
!
$this
->
beforeSave
(
true
))
{
if
(
$runValidation
&&
!
$this
->
validate
(
$attributes
))
{
return
false
;
}
$db
=
static
::
getDb
();
$transaction
=
$this
->
isOperationAtomic
(
self
::
OP_INSERT
)
&&
$db
->
getTransaction
()
===
null
?
$db
->
beginTransaction
()
:
null
;
try
{
$result
=
$this
->
insertInternal
(
$attributes
);
if
(
$transaction
!==
null
)
{
if
(
$result
===
false
)
{
$transaction
->
rollback
();
}
else
{
$transaction
->
commit
();
}
}
}
catch
(
\Exception
$e
)
{
if
(
$transaction
!==
null
)
{
$transaction
->
rollback
();
}
throw
$e
;
}
return
$result
;
}
/**
* @see ActiveRecord::insert()
*/
private
function
insertInternal
(
$attributes
=
null
)
{
if
(
!
$this
->
beforeSave
(
true
))
{
return
false
;
return
false
;
}
}
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
...
@@ -678,7 +723,9 @@ class ActiveRecord extends Model
...
@@ -678,7 +723,9 @@ class ActiveRecord extends Model
}
}
$db
=
static
::
getDb
();
$db
=
static
::
getDb
();
$command
=
$db
->
createCommand
()
->
insert
(
$this
->
tableName
(),
$values
);
$command
=
$db
->
createCommand
()
->
insert
(
$this
->
tableName
(),
$values
);
if
(
$command
->
execute
())
{
if
(
!
$command
->
execute
())
{
return
false
;
}
$table
=
$this
->
getTableSchema
();
$table
=
$this
->
getTableSchema
();
if
(
$table
->
sequenceName
!==
null
)
{
if
(
$table
->
sequenceName
!==
null
)
{
foreach
(
$table
->
primaryKey
as
$name
)
{
foreach
(
$table
->
primaryKey
as
$name
)
{
...
@@ -694,7 +741,6 @@ class ActiveRecord extends Model
...
@@ -694,7 +741,6 @@ class ActiveRecord extends Model
$this
->
afterSave
(
true
);
$this
->
afterSave
(
true
);
return
true
;
return
true
;
}
}
}
/**
/**
* Saves the changes to this active record into the associated database table.
* Saves the changes to this active record into the associated database table.
...
@@ -744,14 +790,46 @@ class ActiveRecord extends Model
...
@@ -744,14 +790,46 @@ class ActiveRecord extends Model
* or [[beforeSave()]] stops the updating process.
* or [[beforeSave()]] stops the updating process.
* @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
* @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
* being updated is outdated.
* being updated is outdated.
* @throws \Exception in case update failed.
*/
*/
public
function
update
(
$runValidation
=
true
,
$attributes
=
null
)
public
function
update
(
$runValidation
=
true
,
$attributes
=
null
)
{
{
if
(
$runValidation
&&
!
$this
->
validate
(
$attributes
)
||
!
$this
->
beforeSave
(
false
))
{
if
(
$runValidation
&&
!
$this
->
validate
(
$attributes
))
{
return
false
;
}
$db
=
static
::
getDb
();
$transaction
=
$this
->
isOperationAtomic
(
self
::
OP_UPDATE
)
&&
$db
->
getTransaction
()
===
null
?
$db
->
beginTransaction
()
:
null
;
try
{
$result
=
$this
->
updateInternal
(
$attributes
);
if
(
$transaction
!==
null
)
{
if
(
$result
===
false
)
{
$transaction
->
rollback
();
}
else
{
$transaction
->
commit
();
}
}
}
catch
(
\Exception
$e
)
{
if
(
$transaction
!==
null
)
{
$transaction
->
rollback
();
}
throw
$e
;
}
return
$result
;
}
/**
* @see CActiveRecord::update()
* @throws StaleObjectException
*/
private
function
updateInternal
(
$attributes
=
null
)
{
if
(
!
$this
->
beforeSave
(
false
))
{
return
false
;
return
false
;
}
}
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
if
(
!
empty
(
$values
))
{
if
(
empty
(
$values
))
{
return
0
;
}
$condition
=
$this
->
getOldPrimaryKey
(
true
);
$condition
=
$this
->
getOldPrimaryKey
(
true
);
$lock
=
$this
->
optimisticLock
();
$lock
=
$this
->
optimisticLock
();
if
(
$lock
!==
null
)
{
if
(
$lock
!==
null
)
{
...
@@ -771,12 +849,8 @@ class ActiveRecord extends Model
...
@@ -771,12 +849,8 @@ class ActiveRecord extends Model
foreach
(
$values
as
$name
=>
$value
)
{
foreach
(
$values
as
$name
=>
$value
)
{
$this
->
_oldAttributes
[
$name
]
=
$this
->
_attributes
[
$name
];
$this
->
_oldAttributes
[
$name
]
=
$this
->
_attributes
[
$name
];
}
}
$this
->
afterSave
(
false
);
$this
->
afterSave
(
false
);
return
$rows
;
return
$rows
;
}
else
{
return
0
;
}
}
}
/**
/**
...
@@ -826,9 +900,14 @@ class ActiveRecord extends Model
...
@@ -826,9 +900,14 @@ class ActiveRecord extends Model
* Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
* Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
* @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
* @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
* being deleted is outdated.
* being deleted is outdated.
* @throws \Exception in case delete failed.
*/
*/
public
function
delete
()
public
function
delete
()
{
{
$db
=
static
::
getDb
();
$transaction
=
$this
->
isOperationAtomic
(
self
::
OP_DELETE
)
&&
$db
->
getTransaction
()
===
null
?
$db
->
beginTransaction
()
:
null
;
try
{
$result
=
false
;
if
(
$this
->
beforeDelete
())
{
if
(
$this
->
beforeDelete
())
{
// we do not check the return value of deleteAll() because it's possible
// 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
// the record is already deleted in the database and thus the method will return 0
...
@@ -837,16 +916,27 @@ class ActiveRecord extends Model
...
@@ -837,16 +916,27 @@ class ActiveRecord extends Model
if
(
$lock
!==
null
)
{
if
(
$lock
!==
null
)
{
$condition
[
$lock
]
=
$this
->
$lock
;
$condition
[
$lock
]
=
$this
->
$lock
;
}
}
$rows
=
$this
->
deleteAll
(
$condition
);
$result
=
$this
->
deleteAll
(
$condition
);
if
(
$lock
!==
null
&&
!
$rows
)
{
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
();
return
$rows
;
}
if
(
$transaction
!==
null
)
{
if
(
$result
===
false
)
{
$transaction
->
rollback
();
}
else
{
}
else
{
return
false
;
$transaction
->
commit
();
}
}
}
catch
(
\Exception
$e
)
{
if
(
$transaction
!==
null
)
{
$transaction
->
rollback
();
}
throw
$e
;
}
}
return
$result
;
}
}
/**
/**
...
@@ -1336,4 +1426,19 @@ class ActiveRecord extends Model
...
@@ -1336,4 +1426,19 @@ class ActiveRecord extends Model
}
}
return
true
;
return
true
;
}
}
/**
* @param string $operation possible values are ActiveRecord::INSERT, ActiveRecord::UPDATE and ActiveRecord::DELETE.
* @return boolean whether given operation is atomic. Currently active scenario is taken into account.
*/
private
function
isOperationAtomic
(
$operation
)
{
$scenario
=
$this
->
getScenario
();
$scenarios
=
$this
->
scenarios
();
if
(
isset
(
$scenarios
[
$scenario
],
$scenario
[
$scenario
][
'atomic'
])
&&
is_array
(
$scenarios
[
$scenario
][
'atomic'
]))
{
return
in_array
(
$operation
,
$scenarios
[
$scenario
][
'atomic'
]);
}
else
{
return
false
;
}
}
}
}
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