45], [['user_agent'], 'string', 'max' => 255], [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']], ]; } /** * {@inheritdoc} */ public function attributeLabels(): array { return [ 'id' => 'ID', 'user_id' => 'User ID', 'ip_address' => 'Ip Address', 'login_time' => 'Login Time', 'user_agent' => 'User Agent', 'status' => 'Status', ]; } /** * Gets query for [[User]]. * * @return ActiveQuery */ public function getUser(): ActiveQuery { return $this->hasOne(User::class, ['id' => 'user_id']); } /** * @param int|null $userId * @param string $ipAddress * @param string $userAgent * @param int $status * @return void */ public static function addLog(int|null $userId, string $ipAddress, string $userAgent, int $status): void { $log = new self(); $log->user_id = $userId??null; $log->ip_address = $ipAddress; $log->login_time = date('Y-m-d H:i:s'); // 使用当前时间作为登录时间 $log->user_agent = strlen($userAgent) > 250 ? substr($userAgent, 0, 250) : $userAgent; $log->status = $status; $log->save(); } }