Stock.php 1.68 KB
<?php
    
    namespace common\modules\product\models;
    
    use Yii;
    use yii\db\ActiveRecord;
    
    /**
     * This is the model class for table "stock".
     *
     * @property integer        $id
     * @property string         $title
     * @property ProductStock[] $productStocks
     */
    class Stock extends ActiveRecord
    {
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'stock';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [ 'title' ],
                    'string',
                    'max' => 150,
                ],
                [
                    [ 'title' ],
                    'required',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'    => Yii::t('product', 'Stock ID'),
                'title' => Yii::t('product', 'Name'),
            ];
        }
        
        public function getProductStocks()
        {
            return $this->hasMany(ProductStock::className(), [ 'stock_id' => 'id' ]);
        }
        
        public function getProductVariants()
        {
            return $this->hasMany(ProductVariant::className(), [ 'id' => 'product_variant_id' ])
                        ->via('productStocks');
        }
        
        public function getProducts()
        {
            return $this->hasMany(Product::className(), [ 'id' => 'product_id' ])
                        ->via('productVariants');
        }
    }