Coder Social home page Coder Social logo

mootensai / yii2-relation-trait Goto Github PK

View Code? Open in Web Editor NEW
46.0 10.0 44.0 115 KB

Yii 2 Models add functionality for load with relation, & transactional save with relation PLUS soft delete/restore feature

PHP 100.00%
relational model softdelete yii2 yii2-extension relational-databases relational-model

yii2-relation-trait's Introduction

yii2-relation-trait

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

PLUS soft delete/restore feature!

Best work with mootensai/yii2-enhanced-gii

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

Support

Support via Gratipay

https://www.paypal.me/yohanesc

Endorse me on LinkedIn

https://www.linkedin.com/in/yohanes-candrajaya-b68394102/

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to the require section of your composer.json file.

Usage At Model

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

Array Input & Usage At Controller

It takes a normal array of POST. This is the example

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)

Use Normal Save

So your behaviors still works

Add Validation At Main Model

$form->errorSummary($model);

will give you

<<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 :

https://github.com/mootensai/yii2-uuid-behavior

Soft Delete

Add this line to your Model to enable soft delete

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

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

I'm open for any improvement

Please create issue if you got a problem or an idea for enhancement

~ SDG ~

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.