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
02594d72
Commit
02594d72
authored
Jul 03, 2013
by
resurtm
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add AR atomic operations and scenarios draft.
parent
f2e57b2e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
126 additions
and
1 deletion
+126
-1
active-record.md
docs/guide/active-record.md
+126
-1
No files found.
docs/guide/active-record.md
View file @
02594d72
...
@@ -468,7 +468,132 @@ can take default values like shown above.
...
@@ -468,7 +468,132 @@ can take default values like shown above.
Atomic operations and scenarios
Atomic operations and scenarios
-------------------------------
-------------------------------
TBD: https://github.com/yiisoft/yii2/issues/226
TODO: FIXME: WIP, TBD, https://github.com/yiisoft/yii2/issues/226
Imagine situation where you have to save something related to the main model in
[
[beforeSave()
]
],
[
[afterSave()
]
],
[
[beforeDelete()
]
] and/or
[
[afterDelete()
]
] life cycle methods. Developer may come
to solution of overriding ActiveRecord
[
[save()
]
] method with database transaction wrapping or
even using transaction in controller action, which is strictly speaking doesn't seems to be a good
practice (recall skinny-controller fat-model fundamental rule).
Here these ways are (
**DO NOT**
use them unless you're sure what are you actually doing). Models:
```
php
class
Feature
extends
\yii\db\ActiveRecord
{
// ...
public
function
getProduct
()
{
return
$this
->
hasOne
(
'Product'
,
array
(
'product_id'
=>
'id'
));
}
}
class
Product
extends
\yii\db\ActiveRecord
{
// ...
public
function
getFeatures
()
{
return
$this
->
hasMany
(
'Feature'
,
array
(
'id'
=>
'product_id'
));
}
}
```
Overriding
[
[save()
]
] method:
```
php
class
ProductController
extends
\yii\web\Controller
{
public
function
actionCreate
()
{
// FIXME: TODO: WIP, TBD
}
}
```
Using transactions within controller layer:
```
php
class
ProductController
extends
\yii\web\Controller
{
public
function
actionCreate
()
{
// FIXME: TODO: WIP, TBD
}
}
```
Instead of using these fragile methods you should consider using atomic scenarios and operations feature.
```
php
class
Feature
extends
\yii\db\ActiveRecord
{
// ...
public
function
getProduct
()
{
return
$this
->
hasOne
(
'Product'
,
array
(
'product_id'
=>
'id'
));
}
public
function
scenarios
()
{
return
array
(
'userCreates'
=>
array
(
'attributes'
=>
array
(
'name'
,
'value'
),
'atomic'
=>
array
(
self
::
OP_INSERT
),
),
);
}
}
class
Product
extends
\yii\db\ActiveRecord
{
// ...
public
function
getFeatures
()
{
return
$this
->
hasMany
(
'Feature'
,
array
(
'id'
=>
'product_id'
));
}
public
function
scenarios
()
{
return
array
(
'userCreates'
=>
array
(
'attributes'
=>
array
(
'title'
,
'price'
),
'atomic'
=>
array
(
self
::
OP_INSERT
),
),
);
}
public
function
afterValidate
()
{
parent
::
afterValidate
();
// FIXME: TODO: WIP, TBD
}
public
function
afterSave
(
$insert
)
{
parent
::
afterSave
();
if
(
$this
->
getScenario
()
===
'userCreates'
)
{
// FIXME: TODO: WIP, TBD
}
}
}
```
Controller is very thin and neat:
```
php
class
ProductController
extends
\yii\web\Controller
{
public
function
actionCreate
()
{
// FIXME: TODO: WIP, TBD
}
}
```
See also
See also
--------
--------
...
...
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