PHP code example of notamedia / yii2-relation

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

    

notamedia / yii2-relation example snippets




class News extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            [['file'], 'safe']
        ];
    }
    
    public function getFile()
    {
        return $this->hasOne(File::class, ['id' => 'file_id']);
    }
    
    public function behaviors()
    {
        return [
            [
                'class' => \notamedia\relation\RelationBehavior::class,
                'relations' => ['file']
            ]
        ];
    }
    
    public function transactions()
    {
        return [
            $this->getScenario() => static::OP_ALL
        ];
    }
}




class News extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            [['images'], 'safe']
        ];
    }
    
    public function getImages()
    {
        return $this->hasMany(Image::class, 
            ['news_id' => 'id']);
    }
    
    public function behaviors()
    {
        return [
            [
                'class' => \notamedia\relation\RelationBehavior::class,
                'relations' => ['images']
            ]
        ];
    }
    
    public function transactions()
    {
        return [
            $this->getScenario() => static::OP_ALL
        ];
    }
}



class News extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            [['categories'], 'safe']
        ];
    }
       
    public function getNewsHasCategories()
    {
        return $this->hasMany(NewsHasCategory::class, ['news_id' => 'id']);
    }

    public function getCategories()
    {
        return $this->hasMany(Category::class, ['id' => 'category_id'])
            ->via('newsHasCategories');
    }
    
    public function behaviors()
    {
        return [
            [
                'class' => \notamedia\relation\RelationBehavior::class,
                'relations' => ['categories']
            ]
        ];
    }
    
    public function transactions()
    {
        return [
            $this->getScenario() => static::OP_ALL
        ];
    }
}



class News extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            [['categories', 'categories_type_archive'], 'safe']
        ];
    }

    public function getCategories()
    {
        return $this->hasMany(Category::class, ['id' => 'category_id'])
            ->viaTable('news_has_categories', ['news_id' => 'id']);
    }
    
    // with onCondition
    public function getCategories_type_archive()
    {
        return $this->hasMany(Category::class, ['id' => 'category_id'])
            ->viaTable('news_has_categories', ['news_id' => 'id'], function($query) {
                 return $query->onCondition(['type' => 'archive']);
            });
    }
    
    public function behaviors()
    {
        return [
            [
                'class' => \notamedia\relation\RelationBehavior::class,
                'relations' => ['categories', 'categories_type_archive']
            ]
        ];
    }
    
    public function transactions()
    {
        return [
            $this->getScenario() => static::OP_ALL
        ];
    }
}



class News extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            [['categories'], 'safe']
        ];
    }
       
    public function getNewsHasCategories()
    {
        return $this->hasMany(NewsHasCategory::class, ['news_id' => 'id']);
    }
    
    public function getCategories()
    {
        return $this->hasMany(Category::class, ['id' => 'category_id'])
            ->via('newsHasCategories');
    }
    
    public function behaviors()
    {
        $sortIndex = 1;
        
        return [
            [
                'class' => \notamedia\relation\RelationBehavior::class,
                'relations' => [
                    'categories' => function (NewsHasCategory $model) use (&$sortIndex) {
                        $model->sort = $sortIndex++;
    
                        return $model;
                    }
                ]
            ]
        ];
    }
    
    public function transactions()
    {
        return [
            $this->getScenario() => static::OP_ALL
        ];
    }
}