<?php
    
    namespace common\models;
    
    use common\modules\product\models\Product;
    use common\modules\product\models\ProductVariant;
    use Yii;
    
    /**
     * This is the model class for table "product_to_project".
     * @property integer        $product_to_project_id
     * @property integer        $product_variant_id
     * @property integer        $project_id
     * @property ProductVariant $productVariant
     * @property Project        $project
     * @property Product        $product
     */
    class ProductToProject extends \yii\db\ActiveRecord
    {
        
        public $product_id;
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'product_to_project';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'product_variant_id',
                        'project_id',
                        'product_id',
                    ],
                    'required',
                ],
                [
                    [
                        'product_variant_id',
                        'product_id',
                    ],
                    'integer',
                ],
                [
                    [
                        'product_variant_id',
                        'project_id',
                    ],
                    'unique',
                    'targetAttribute' => [
                        'product_variant_id',
                        'project_id',
                    ],
                    'message'         => 'The combination of Product Variant ID and Project ID has already been taken.',
                ],
                [
                    [ 'product_variant_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => ProductVariant::className(),
                    'targetAttribute' => [ 'product_variant_id' => 'product_variant_id' ],
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'product_to_project_id' => Yii::t('app', 'product_to_project_id'),
                'product_variant_id'    => Yii::t('app', 'product_variant_id'),
                'project_id'            => Yii::t('app', 'project_id'),
                'product_id'            => Yii::t('app', 'product_id'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProductVariant()
        {
            return $this->hasOne(ProductVariant::className(), [ 'product_variant_id' => 'product_variant_id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProject()
        {
            return $this->hasOne(Project::className(), [ 'project_id' => 'project_id' ]);
        }
        
        public function getProduct()
        {
            return $this->hasOne(Product::className(), [
                'product_id' => 'product_id',
            ])
                        ->viaTable('product_variant as pv', [ 'product_variant_id' => 'product_variant_id' ]);
        }
    }
