2017 © Pedro Peláez
 

yii2-extension yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

image

deadmantfa/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  • Wednesday, February 14, 2018
  • by deadmantfa
  • Repository
  • 1 Watchers
  • 0 Stars
  • 33 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 38 Forks
  • 0 Open issues
  • 29 Versions
  • 3 % Grown

The README.md

yii2-relation-trait

I am not the creator of this extension, I have made a bug fix and made a pull request. I am just publishing this for my own usage. Feel free to use it.

All thanks to @mootensai for creating the extension.

This is not to replace the package maintained by @mootensai

Yii 2 Models add functionality for load with relation (loadAll($POST)), & transactional save with relation (saveAll()), (*1)

PLUS soft delete/restore feature!, (*2)

Best work with mootensai/yii2-enhanced-gii, (*3)

Latest Stable Version License Total Downloads Monthly Downloads Daily Downloads Join the chat at https://gitter.im/mootensai/yii2-relation-trait, (*4)

Support

Support via Gratipay, (*5)

https://www.paypal.me/yohanesc, (*6)

Endorse me on LinkedIn, (*7)

https://www.linkedin.com/in/yohanes-candrajaya-b68394102/, (*8)

Installation

The preferred way to install this extension is through composer., (*9)

Either run, (*10)

$ composer require 'deadmantfa/yii2-relation-trait:dev-master'

or add, (*11)

"deadmantfa/yii2-relation-trait": "*"

to the require section of your composer.json file., (*12)

Usage At Model

class MyModel extends ActiveRecord{
    use \deadmantfa\relation\RelationTrait;
}

Array Input & Usage At Controller

It takes a normal array of POST. This is the example, (*13)

Array (
    $_POST['ParentClass'] => Array 
        (
            [attr1] => value1
            [attr2] => value2 
            // has many
            [relationName] => Array 
                ( 
                    [0] => Array 
                        (
                            [relAttr] => relValue1
                        )
                    [1] => Array 
                        (
                            [relAttr] => relValue1
                        )
                )
            // has one
            [relationName] => Array
                ( 
                    [relAttr1] => relValue1
                    [relAttr2] => relValue2
                )
        )
)

OR

Array (
    $_POST['ParentClass'] => ['attr1' => 'value1','attr2' => 'value2'],
    // Has One
    $_POST['RelatedClass'] => ['relAttr1' => 'value1','relAttr2' => 'value2'], 
    // Has Many
    $_POST['RelatedClass'] => Array
        (
            [0] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
            [1] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
        )      
)
// sample at controller
if($model->loadAll(Yii:$app->request->post()) && $model->saveAll()){
    return $this->redirect(['view', 'id' => $model->id, 'created' => $model->created]);
}

Features

Array Output

// I use this to send model & related through JSON / Serialize
print_r($model->getAttributesWithRelatedAsPost());
Array
(
    [MainClass] => Array
        (
            [attr1] => value1
            [attr2] => value2
        )

    [RelatedClass] => Array
        (
            [0] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
        )

)
print_r($model->getAttributesWithRelated());
Array
(
    [attr1] => value1
    [attr2] => value2
    [relationName] => Array
        (
            [0] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
        )
)

Use Transaction

So your data will be atomic (see : http://en.wikipedia.org/wiki/ACID), (*14)

Use Normal Save

So your behaviors still works, (*15)

Add Validation At Main Model

$form->errorSummary($model);

will give you, (*16)

<<Related Class Name>> #<<index + 1>> : <<error message>>
My Related Model #1 : Attribute is required

It Works On Auto Incremental PK Or Not (I Have Tried Use UUID)

See here if you want to use my behavior :, (*17)

https://github.com/mootensai/yii2-uuid-behavior, (*18)

Soft Delete

Add this line to your Model to enable soft delete, (*19)

private $_rt_softdelete;

function __construct(){
    $this->_rt_softdelete = [
        '<column>' => <undeleted row marker value>
        // multiple row marker column example
        'isdeleted' => 1,
        'deleted_by' => \Yii::$app->user->id,
        'deleted_at' => date('Y-m-d H:i:s')
    ];
}

Add this line to your Model to enable soft restore, (*20)

private $_rt_softrestore;

function __construct(){
    $this->_rt_softrestore = [
        '<column>' => <undeleted row marker value>
        // multiple row marker column example
        'isdeleted' => 0,
        'deleted_by' => 0,
        'deleted_at' => 'NULL'
    ];
}

Should work on Yii's supported DB

It use all Yii's Active Query or Active Record to execute DB command, (*21)

I'm open for any improvement

Please create issue if you got a problem or an idea for enhancement, (*22)

~ SDG ~

The Versions

14/02 2018

dev-master

9999999-dev http://github.com/deadmantfa/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya
by Wenceslaus Dsilva

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

14/02 2018

1.1.15

1.1.15.0 http://github.com/deadmantfa/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya
by Wenceslaus Dsilva

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

11/02 2018

1.1.14

1.1.14.0 http://github.com/deadmantfa/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya
by Wenceslaus Dsilva

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

11/02 2018

1.1.13

1.1.13.0 http://github.com/deadmantfa/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya
by Wenceslaus Dsilva

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

09/02 2018

1.1.12

1.1.12.0 http://github.com/deadmantfa/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya
by Wenceslaus Dsilva

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

09/02 2018

1.1.11

1.1.11.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya
by Wenceslaus Dsilva

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

09/02 2018

1.1.10.1

1.1.10.1 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

09/02 2018

1.1.10

1.1.10.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

09/02 2018

1.1.9

1.1.9.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

18/10 2017

1.1.8

1.1.8.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

28/09 2017

1.1.7

1.1.7.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

23/07 2017

1.1.6

1.1.6.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

12/07 2017

1.1.5

1.1.5.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

12/07 2017

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

08/07 2016

1.1.4

1.1.4.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

11/06 2016

1.1.3

1.1.3.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

08/06 2016

1.1.2

1.1.2.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

26/02 2016

1.1.1

1.1.1.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

24/02 2016

1.1.0

1.1.0.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

22/02 2016

1.0.9

1.0.9.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

17/02 2016

1.0.8

1.0.8.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

27/07 2015

1.0.7

1.0.7.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

24/07 2015

1.0.6

1.0.6.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

08/07 2015

1.0.5

1.0.5.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

10/06 2015

1.0.4

1.0.4.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

09/06 2015

1.0.3

1.0.3.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

09/06 2015

1.0.2

1.0.2.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

09/06 2015

1.0.1

1.0.1.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall

03/06 2015

1.0.0

1.0.0.0 http://github.com/mootensai/yii2-relation-trait

Yii 2 Models load with relation, & transaction save with relation

  Sources   Download

BSD-3-Clause

The Requires

 

by Yohanes Candrajaya

yii2 load related transaction save relation loadwithrelation savewithrelation saveall loadall