PHP code example of mootensai / yii2-relation-trait

1. Go to this page and download the library: Download mootensai/yii2-relation-trait library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mootensai / yii2-relation-trait example snippets


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

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]);
}

// I use this to send model & related through JSON / Serialize
print_r($model->getAttributesWithRelatedAsPost());

print_r($model->getAttributesWithRelated());

$form->errorSummary($model);

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')
    ];
}

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'
    ];
}