[ 'class' => BlameableBehavior::className(), 'createdByAttribute' => 'user_id', 'updatedByAttribute' => false, ], [ 'class' => AttributeBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_AFTER_FIND => 'visited_at', ], 'value' => function ($event) { return time(); }, ], ]; } /** * @inheritdoc */ public function rules() { return [ [ [ 'name', 'surname', 'image', 'email', 'phone', ], 'string', 'max' => 255, ], [ [ 'email' ], 'email', ], [ [ 'phone' ], 'match', 'pattern' => '/^\+\d{1,3}\(\d+\)\d+-\d+-\d+$/', ], [ [ 'user_id' ], 'unique', ], [ [ 'user_id' ], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => [ 'user_id' => 'id' ], ], [ [ 'image' ], 'filter', 'filter' => function ($value) { if (!$value) { return null; } else { return $value; } }, ], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'user_id' => Yii::t('core', 'User ID'), 'name' => Yii::t('core', 'Name'), 'surname' => Yii::t('core', 'Surname'), 'image' => Yii::t('core', 'Image'), 'email' => Yii::t('core', 'Email'), 'phone' => Yii::t('core', 'Phone'), 'created_at' => Yii::t('core', 'Created At'), 'updated_at' => Yii::t('core', 'Updated At'), 'visited_at' => Yii::t('core', 'Visited At'), ]; } /** * @return \yii\db\ActiveQuery */ public function getUser() { return $this->hasOne(User::className(), [ 'id' => 'user_id' ]); } /** * Get fullname * * @return string */ public function getFullname() { if (empty( $this->name ) && empty( $this->surname )) { return \Yii::t('core', 'Anonymous'); } else { return implode( ' ', [ $this->name ? : \Yii::t('core', 'Mr.'), $this->surname, ] ); } } }