PHP code example of marena / yii2-relation-trait

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

    

marena / yii2-relation-trait example snippets


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

// sample at controller
//$_POST['ParentClass'] = ['attr1' => 'value1','attr2' => 'value2'];
//$_POST['RelatedClass'][0] = ['attr1' => 'value1','attr2' => 'value2'];      
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);