RatingModel.php 2.53 KB
<?php
    
    namespace artbox\webcomment\models;
    
    use Yii;
    use yii\behaviors\BlameableBehavior;
    use yii\behaviors\TimestampBehavior;
    use yii\db\ActiveRecord;
    
    /**
     * This is the model class for table "artbox_comment_rating".
     *
     * @property integer $id
     * @property string  $created_at
     * @property string  $updated_at
     * @property integer $customer_id
     * @property integer $value
     * @property string  $model
     * @property integer $model_id
     */
    class RatingModel extends ActiveRecord
    {
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'artbox_comment_rating';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [ 'value' ],
                    'required',
                ],
                [
                    [ 'value' ],
                    'number',
                    'min' => 0.5,
                    'max' => 5,
                ],
            ];
        }
        
        public function behaviors()
        {
            return [
                [
                    'class' => TimestampBehavior::className(),
                ],
                [
                    'class'              => BlameableBehavior::className(),
                    'createdByAttribute' => 'customer_id',
                    'updatedByAttribute' => false,
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'          => Yii::t('app', 'Rating ID'),
                'created_at'  => Yii::t('app', 'Date Add'),
                'updated_at'  => Yii::t('app', 'Date Update'),
                'customer_id' => Yii::t('app', 'User ID'),
                'entity'      => Yii::t('app', 'Entity'),
                'value'       => Yii::t('app', 'Value'),
            ];
        }
    
        /**
         * Get entity model for current model
         *
         * @return \yii\db\ActiveQuery|null
         */
        public function getModel()
        {
            $model = $this->model;
            if (method_exists($model, 'primaryKey')) {
                /**
                 * @var ActiveRecord $model
                 */
                return $this->hasOne($model, [ $model::primaryKey()[ 0 ] => 'model_id' ]);
            } else {
                return null;
            }
        }
    }