ProductStock.php 3.23 KB
<?php
    
    namespace common\modules\product\models;
    
    use yii\db\ActiveRecord;
    
    /**
     * This is the model class for table "product_stock".
     *
     * @property integer        $stock_id
     * @property integer        $quantity
     * @property integer        $product_variant_id
     * @property Product        $product
     * @property ProductVariant $productVariant
     * @property Stock          $stock
     */
    class ProductStock extends ActiveRecord
    {
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'product_stock';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'stock_id',
                        'quantity',
                        'product_variant_id',
                    ],
                    'integer',
                ],
                [
                    [ 'title' ],
                    'required',
                ],
                [
                    [ 'product_variant_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => ProductVariant::className(),
                    'targetAttribute' => [ 'product_variant_id' => 'id' ],
                ],
                [
                    [ 'stock_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Stock::className(),
                    'targetAttribute' => [ 'stock_id' => 'id' ],
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'stock_id'           => 'Stock ID',
                'quantity'           => 'Количество',
                'product_variant_id' => 'Product Variant ID',
                'title'              => "Название",
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProduct()
        {
            return $this->hasOne(Product::className(), [ 'id' => 'product_id' ])
                        ->via('variants');
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProductVariant()
        {
            return $this->hasOne(ProductVariant::className(), [ 'id' => 'product_variant_id' ]);
        }
        
        public function getName()
        {
            return ( !empty( $this->stock ) ) ? $this->stock->title : '';
        }
        
        /**
         * @todo Check if needed
         *
         * @param mixed $value
         */
        public function setName($value)
        {
            $this->title = $value;
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getStock()
        {
            return $this->hasOne(Stock::className(), [ 'id' => 'stock_id' ]);
        }
        
        public static function primaryKey()
        {
            return [
                "stock_id",
                "product_variant_id",
            ];
        }
    }