ProductToImage.php 2.1 KB
<?php
    
    namespace artbox\catalog\models;

    use artbox\core\models\Image;
    use Yii;
    use yii\db\ActiveRecord;

    /**
     * This is the model class for table "product_to_image".
     
*
     * @property integer $product_id
     * @property integer $image_id
     * @property Image   $image
     * @property Product $product
     */
    class ProductToImage extends ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'product_to_image';
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'product_id',
                        'image_id',
                    ],
                    'required',
                ],
                [
                    [
                        'product_id',
                        'image_id',
                    ],
                    'integer',
                ],
                [
                    [ 'product_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Product::className(),
                    'targetAttribute' => [ 'product_id' => 'id' ],
                ],
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'product_id' => Yii::t('catalog', 'Product ID'),
                'image_id'   => Yii::t('catalog', 'Image ID'),
            ];
        }
    
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getImage()
        {
            return $this->hasOne(Image::className(), [ 'id' => 'image_id' ])
                        ->inverseOf('productToImages');
        }
    
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProduct()
        {
            return $this->hasOne(Product::className(), [ 'id' => 'product_id' ])
                        ->inverseOf('productToImages');
        }
    }