All Projects → mootensai → Yii2 Relation Trait

mootensai / Yii2 Relation Trait

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

Projects that are alternatives of or similar to Yii2 Relation Trait

Yii2 Enhanced Gii
Enhanced Yii2 Gii (generator) that generates related Models & CRUD
Stars: ✭ 183 (+289.36%)
Mutual labels:  yii2, yii2-extension, model
yii2-queuemanager
Yii2 Queue Manager (Analytic & Monitor)
Stars: ✭ 18 (-61.7%)
Mutual labels:  yii2, yii2-extension
Yii2 User
[ABANDONED] Flexible user registration and authentication module for Yii2
Stars: ✭ 946 (+1912.77%)
Mutual labels:  yii2, yii2-extension
Yii2 Helpers
Collection of useful helper functions for Yii Framework 2.0
Stars: ✭ 16 (-65.96%)
Mutual labels:  yii2, yii2-extension
collection
Basic collection library for Yii Framework 2.0
Stars: ✭ 29 (-38.3%)
Mutual labels:  yii2, yii2-extension
Yii2 Cms
Simple CMS extension
Stars: ✭ 42 (-10.64%)
Mutual labels:  yii2, yii2-extension
Yii2 Elfinder
elFinder file manager for Yii 2
Stars: ✭ 21 (-55.32%)
Mutual labels:  yii2, yii2-extension
yii2-firebird
Firebird connector for Yii2 framework
Stars: ✭ 23 (-51.06%)
Mutual labels:  yii2, yii2-extension
Yii2 Selectize
selectize.js wrapper for yii2.
Stars: ✭ 18 (-61.7%)
Mutual labels:  yii2, yii2-extension
Yii2 Image Attachment
This extension intended to handle images associated with model.
Stars: ✭ 35 (-25.53%)
Mutual labels:  yii2, yii2-extension
Yii2 Telegram Log
Telegram log target for Yii 2
Stars: ✭ 24 (-48.94%)
Mutual labels:  yii2, yii2-extension
Yii2 Many To Many
Implementation of Many-to-many relationship for Yii 2 framework
Stars: ✭ 30 (-36.17%)
Mutual labels:  yii2, yii2-extension
yii2-blog
Simple, configurable blog module for Yii2 (post, comment, nested category, tags). + frontend, backend. + SEO! (Opengraph, Schema.org) ~~~COMING SOON V2.0~~~ Please STAR this repo
Stars: ✭ 79 (+68.09%)
Mutual labels:  yii2, yii2-extension
Yii2 Gravatar
Gravatar Widget for Yii Framework 2
Stars: ✭ 36 (-23.4%)
Mutual labels:  yii2, yii2-extension
yii2-jwt-user
JWT (JSON Web Token) User component for Yii 2
Stars: ✭ 16 (-65.96%)
Mutual labels:  yii2, yii2-extension
Yii2 Gentelella
Free admin template for backend
Stars: ✭ 266 (+465.96%)
Mutual labels:  yii2, yii2-extension
Yii2 C3 Chart
Yii2 wrapper for D3-based reusable chart library
Stars: ✭ 9 (-80.85%)
Mutual labels:  yii2, yii2-extension
yii2-star-rating
Star rating widget based on jQuery Raty
Stars: ✭ 16 (-65.96%)
Mutual labels:  yii2, yii2-extension
yii2-facades
Facades for Yii 2
Stars: ✭ 21 (-55.32%)
Mutual labels:  yii2, yii2-extension
Yii2 Google Maps Markers
Google Maps Markers Widget for Yii2
Stars: ✭ 16 (-65.96%)
Mutual labels:  yii2, yii2-extension

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 ~

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].