<?php
    
    namespace common\models;
    
    use common\modules\language\models\Language;
    use Yii;
    use yii\db\ActiveRecord;
    
    /**
     * This is the model class for table "project_lang".
     *
     * @property integer  $project_id
     * @property integer  $language_id
     * @property string   $title
     * @property string   $description
     * @property string   $description_preview
     * @property string   $alias
     * @property Language $language
     * @property Project  $project
     */
    class ProjectLang extends ActiveRecord
    {
        
        public static function primaryKey()
        {
            return [
                'project_id',
                'language_id',
            ];
        }
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'project_lang';
        }
        
        public function behaviors()
        {
            return [
                'slug' => [
                    'class'         => 'common\behaviors\Slug',
                    'in_attribute'  => 'title',
                    'out_attribute' => 'alias',
                    'translit'      => true,
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [ 'title' ],
                    'required',
                ],
                [
                    [
                        'description',
                        'description_preview',
                    ],
                    'string',
                ],
                [
                    [
                        'title',
                        'alias',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [
                        'project_id',
                        'language_id',
                    ],
                    'unique',
                    'targetAttribute' => [
                        'project_id',
                        'language_id',
                    ],
                    'message'         => 'The combination of Project ID and Language ID has already been taken.',
                ],
                [
                    [ 'language_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Language::className(),
                    'targetAttribute' => [ 'language_id' => 'language_id' ],
                ],
                [
                    [ 'project_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Project::className(),
                    'targetAttribute' => [ 'project_id' => 'project_id' ],
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'project_id'          => Yii::t('app', 'project_id'),
                'language_id'         => Yii::t('app', 'language_id'),
                'title'               => Yii::t('app', 'title'),
                'description'         => Yii::t('app', 'description'),
                'description_preview' => Yii::t('app', 'description_preview'),
                'alias'               => Yii::t('app', 'alias'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getLanguage()
        {
            return $this->hasOne(Language::className(), [ 'language_id' => 'language_id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProject()
        {
            return $this->hasOne(Project::className(), [ 'project_id' => 'project_id' ]);
        }
    }
