PHP code example of deadmantfa / yii2-relation-trait

1. Go to this page and download the library: Download deadmantfa/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/ */

    

deadmantfa / yii2-relation-trait example snippets


"deadmantfa/yii2-relation-trait": "^2.0.0"

use deadmantfa\relation\RelationTrait;

class MyModel extends \yii\db\ActiveRecord
{
    use RelationTrait;

    // ...
}

[
    $_POST['ParentClass'] => [
        'attr1' => 'value1',
        'attr2' => 'value2',
        // Has many
        'relationName' => [
            [ 'relAttr' => 'relValue1' ],
            [ 'relAttr' => 'relValue2' ]
        ],
        // Has one
        'relationName' => [
            'relAttr1' => 'relValue1',
            'relAttr2' => 'relValue2'
        ]
    ]
];

$model = new ParentClass();
if ($model->loadAll(Yii::$app->request->post()) && $model->saveAll()) {
    return $this->redirect(['view', 'id' => $model->id]);
}

        private $_rt_softdelete;
        private $_rt_softrestore;
    
        public function __construct($config = [])
        {
            parent::__construct($config);
    
            $this->_rt_softdelete = [
                'is_deleted' => 1,
                'deleted_by' => Yii::$app->user->id,
                'deleted_at' => date('Y-m-d H:i:s'),
            ];
    
            $this->_rt_softrestore = [
                'is_deleted' => 0,
                'deleted_by' => null,
                'deleted_at' => null,
            ];
        }
    

print_r($model->getAttributesWithRelatedAsPost());

print_r($model->getAttributesWithRelated());