Payment.php 3.63 KB
<?php
    
    namespace artbox\order\labels\models;
    
    use artbox\core\behaviors\LanguageBehavior;
    use artbox\core\models\Language;
    use artbox\order\models\Order;
    use Yii;
    use yii\db\ActiveQuery;
    use yii\db\ActiveRecord;
    use yii2tech\ar\position\PositionBehavior;
    use yii2tech\ar\variation\VariationBehavior;
    
    /**
     * This is the model class for table "payment".
     *
     * @property integer                      $id
     * @property integer                      $sort
     * @property boolean                      $status
     * @property boolean                      $default
     * @property \artbox\order\models\Order[] $orders
     * @property PaymentLang[]                $paymentLangs
     * @property Language[]                   $languages
     * @see LanguageBehavior
     */
    class Payment extends ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'payment';
        }
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'translations'     => [
                    'class'                             => VariationBehavior::className(),
                    'variationsRelation'                => 'languages',
                    'defaultVariationRelation'          => 'language',
                    'variationOptionReferenceAttribute' => 'language_id',
                    'optionModelClass'                  => Language::className(),
                    'defaultVariationOptionReference'   => function () {
                        return Language::getCurrent()->id;
                    },
                    'optionQueryFilter'                 => function (ActiveQuery $query) {
                        $query->where(
                            [
                                'status' => true,
                            ]
                        );
                    },
                ],
                'positionBehavior' => [
                    'class'             => PositionBehavior::className(),
                    'positionAttribute' => 'sort',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [ 'sort' ],
                    'integer',
                ],
                [
                    [ 'status' ],
                    'boolean',
                ],
                [
                    [ 'default' ],
                    'boolean',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'      => Yii::t('order', 'ID'),
                'sort'    => Yii::t('order', 'Sort'),
                'status'  => Yii::t('order', 'Status'),
                'default' => Yii::t('order', 'Default'),
                'title'   => Yii::t('order', 'Title'),
            ];
        }
        
        public function getLanguages()
        {
            return $this->hasMany(PaymentLang::className(), [ 'payment_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getLanguage()
        {
            return $this->hasOne(PaymentLang::className(), [ 'payment_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getOrders()
        {
            return $this->hasMany(Order::className(), [ 'payment_id' => 'id' ]);
        }
        
    }