OrderProduct.php 2.85 KB
<?php
    
    namespace artbox\order\models;
    
    use artbox\catalog\models\Variant;
    use Yii;
    
    /**
     * This is the model class for table "order_product".
     *
     * @property integer $id
     * @property integer $order_id
     * @property integer $variant_id
     * @property string  $sku
     * @property string  $price
     * @property integer $count
     * @property Order   $order
     * @property Variant $variant
     */
    class OrderProduct extends \yii\db\ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'order_product';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'order_id',
                        'sku',
                    ],
                    'required',
                ],
                [
                    [
                        'order_id',
                        'variant_id',
                        'count',
                    ],
                    'integer',
                ],
                [
                    [ 'price' ],
                    'number',
                ],
                [
                    [ 'sku' ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'order_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Order::className(),
                    'targetAttribute' => [ 'order_id' => 'id' ],
                ],
                [
                    [ 'variant_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Variant::className(),
                    'targetAttribute' => [ 'variant_id' => 'id' ],
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'         => Yii::t('order', 'ID'),
                'order_id'   => Yii::t('order', 'Order ID'),
                'variant_id' => Yii::t('order', 'Variant ID'),
                'sku'        => Yii::t('order', 'Sku'),
                'price'      => Yii::t('order', 'Price'),
                'count'      => Yii::t('order', 'Count'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getOrder()
        {
            return $this->hasOne(Order::className(), [ 'id' => 'order_id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getVariant()
        {
            return $this->hasOne(Variant::className(), [ 'id' => 'variant_id' ]);
        }
    }