BlogCategory.php 3.12 KB
<?php
    
    namespace common\modules\blog\models;
    
    use yii\db\ActiveRecord;
    use common\modules\language\behaviors\LanguageBehavior;
    
    /**
     * This is the model class for table "blog_category".
     *
     * @property integer                 $id
     * @property integer                 $sort
     * @property string                  $image
     * @property integer                 $parent_id
     * @property boolean                 $status
     * @property BlogArticleToCategory[] $blogArticleToCategories
     * @property BlogArticle[]           $blogArticles
     * @property BlogCategoryLang[]      $blogCategoryLangs
     * @property Language[]              $languages
     */
    class BlogCategory extends ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'blog_category';
        }
    
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'language' => [
                    'class' => LanguageBehavior::className(),
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'sort',
                        'parent_id',
                    ],
                    'integer',
                ],
                [
                    [ 'status' ],
                    'boolean',
                ],
                [
                    [ 'image' ],
                    'string',
                    'max' => 255,
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'        => 'ID',
                'sort'      => 'Sort',
                'image'     => 'Image',
                'parent_id' => 'Parent ID',
                'status'    => 'Status',
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getBlogArticleToCategories()
        {
            return $this->hasMany(BlogArticleToCategory::className(), [ 'blog_category_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getBlogArticles()
        {
            return $this->hasMany(BlogArticle::className(), [ 'id' => 'blog_article_id' ])
                        ->viaTable('blog_article_to_category', [ 'blog_category_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getBlogCategoryLangs()
        {
            return $this->hasMany(BlogCategoryLang::className(), [ 'blog_category_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getLanguages()
        {
            return $this->hasMany(Language::className(), [ 'id' => 'language_id' ])
                        ->viaTable('blog_category_lang', [ 'blog_category_id' => 'id' ]);
        }
    }