Comments (3)
Hi @douglasgsouza
The issue should be fixed by 1.7.2 release.
Thank you for your patience and support.
from yii2-save-relations-behavior.
Did you activate the transaction on your owner model?
If not, that’s mandatory in order to correctly rollback related models.
from yii2-save-relations-behavior.
Did you activate the transaction on your owner model?
If not, that’s mandatory in order to correctly rollback related models.
Yes. I used transactions() OP_ALL on owner model.
See more detailed:
Owner Model:
<?php
namespace app\models;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
use yii\behaviors\OptimisticLockBehavior;
use yii\db\ActiveRecord;
/**
* This is the model class for table "sale".
*
* @property int $id
* @property string $sale_date
* @property string $contract
* @property int $customer_id
* @property int $version
* @property Customer $customer
* ... another properties
*/
class Sale extends ActiveRecord
{
public static function tableName()
{
return 'sale';
}
public function rules()
{
return [
[['sale_date', 'contract', 'customer_id'], 'required'],
[['sale_date', 'customer'], 'safe'],
[['customer_id', 'version'], 'integer'],
[['contract'], 'string', 'max' => 20],
[['status'], 'string', 'max' => 1],
[['customer_id'], 'exist', 'skipOnError' => true, 'targetClass' => Customer::class, 'targetAttribute' => ['customer_id' => 'id']],
];
}
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['saveRelations'] = [
'class' => SaveRelationsBehavior::class,
'relations' => [
'customer',
],
];
$behaviors['optimisticLock'] => [
'class' => OptimisticLockBehavior::class,
'skipUpdateOnClean' => true
];
return $behaviors;
}
public function optimisticLock()
{
return 'version';
}
public function getCustomer()
{
return $this->hasOne(Customer::class, ['id' => 'customer_id']);
}
public function transactions()
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}
}
Related Model:
<?php
namespace app\models;
use yii\behaviors\OptimisticLockBehavior;
use yii\db\ActiveRecord;
/**
* This is the model class for table "customer".
*
* @property int $id
* @property string $name
* @property string $document
* @property int $version
* ... another properties
*/
class Customer extends ActiveRecord
{
public static function tableName()
{
return 'customer';
}
public function rules()
{
return [
[['name', 'document'], 'required'],
[['version'], 'integer'],
[['name'], 'string', 'max' => 255],
[['document'], 'string', 'max' => 14]
];
}
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['optimisticLock'] => [
'class' => OptimisticLockBehavior::class,
'skipUpdateOnClean' => true
];
return $behaviors;
}
public function optimisticLock()
{
return 'version';
}
public function transactions()
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}
}
Test:
$sale = new Sale();
$sale->sale_date = '2019-04-02';
//$sale->contract = '123'; //intentionally the mandatory property is not filled to cause validation error in the owner model
$sale->customer = ['name' => 'test customer', 'document' => '1234']; // Will create a new customer record
$sale->save();
Program Execution Steps
$sale->save();
SaveRelationsBehavior->beforeValidate()
SaveRelationsBehavior->beforeValidate()->_prepareHasOneRelation()
SaveRelationsBehavior->beforeValidate()->_prepareHasOneRelation()-> call related save();
SaveRelationsBehavior->beforeValidate()->_prepareHasOneRelation()-> open Transaction
SaveRelationsBehavior->beforeValidate()->_prepareHasOneRelation()-> insert into DB
SaveRelationsBehavior->beforeValidate()->_prepareHasOneRelation()-> commit transaction
$sale->save(); -> continues and call model validate()
$sale->save(); -> return false, Model not inserted due to validation error.
A validation error occurred in the owner model, so the owner record was not entered into the database, but the child record was inserted into the database because it is inserted before and is not deleted when a validation error occurs in the owner model.
from yii2-save-relations-behavior.
Related Issues (20)
- Updating multiple records in nested related model HOT 3
- Add the ability to retrieve old relations values
- Add the ability to retrieve and mark dirty relations
- "private" replace with "protected".
- Junction Table Extra Columns forces delete/insert on existing records
- usage question maybe a bug? HOT 3
- feature request - work only existing models, or create new
- 1:1 connection - feature request
- hasOne relation via() is not working as expected
- Error Unable to link models: the primary key of
- Nested model changes does not save if parent model not dirty. HOT 1
- Only write, not rewrite
- Don't work with some relations HOT 5
- Fail when parent model has more than one PRIMARY KEY
- Using behavior for relations HOT 1
- Load relations with Json Data
- Enh: Аdjust access to save the relation HOT 4
- unlink and link should also use the via on conditions of the relation
- Unitentionally triggered afterSave on related model
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yii2-save-relations-behavior.