ProductRecommend.php 2.68 KB
<?php
    
    namespace artbox\catalog\models;
    
    use Yii;
    use yii\db\ActiveRecord;
    
    /**
     * This is the model class for table "product_recommend".
     *
     * @property integer $product_id
     * @property integer $recommend_id
     * @property Product $product
     * @property Product $recommend
     */
    class ProductRecommend extends ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'product_recommend';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'product_id',
                        'recommend_id',
                    ],
                    'required',
                ],
                [
                    [
                        'product_id',
                        'recommend_id',
                    ],
                    'integer',
                ],
                [
                    [ 'product_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Product::className(),
                    'targetAttribute' => [ 'product_id' => 'id' ],
                ],
                [
                    [ 'recommend_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Product::className(),
                    'targetAttribute' => [ 'recommend_id' => 'id' ],
                ],
                [
                    [
                        'product_id',
                        'recommend_id',
                    ],
                    'unique',
                    'targetAttribute' => [
                        'product_id',
                        'recommend_id',
                    ],
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'product_id'   => Yii::t('catalog', 'Product ID'),
                'recommend_id' => Yii::t('catalog', 'Recommend ID'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProduct()
        {
            return $this->hasOne(Product::className(), [ 'id' => 'product_id' ])
                        ->inverseOf('productRecommends');
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getRecommend()
        {
            return $this->hasOne(Product::className(), [ 'id' => 'recommend_id' ]);
        }
    }