Giter Site home page Giter Site logo

Comments (3)

juban avatar juban commented on May 26, 2024 1

Hi @douglasgsouza
The issue should be fixed by 1.7.2 release.
Thank you for your patience and support.

from yii2-save-relations-behavior.

juban avatar juban commented on May 26, 2024

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.

douglasgsouza avatar douglasgsouza commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.