php处理用户,php – 如何处理复杂的用户状态?

有趣的问题,但也没有一个单一的答案.

我认为这里的复杂性可能来自周围的代码,而不是核心业务逻辑和需求.我这样说是因为三种状态类型都是从内部应用程序派生出来的,并不是太糟糕.

一种可能的解决方案,我假设某种程度的MVC或类似.

鉴于你的模型,用户,并扩展像Eloquent这样的ORM(我会从Laravel Eloquent,因为我最熟悉它,但任何ORM都可以工作):

use Illuminate\Database\Eloquent\Model;

use App\DebtCollector;

public class User extends Model

{

// Assuming model has the following fields

// id, status, registration_date, and a one to many

// relationship with debts

protected $fillable = [

'raw_status',

'registration_date',

];

public function debts()

{

return $this->hasMany(Debt::class);

}

public function refreshStatus()

{

$dc = new DebtCollector();

// Business logic inside the "DebtCollector" class

$this->raw_status = $dc->resolveStatus($this->debts, $this->registration_date);

// Save value to the underlying datebase

$this->save();

}

// If you fetch a status directly, it will refresh first,

// then return the value

//

public function getStatusAttribute()

{

$this->refreshStatus();

return $this->raw_status;

}

}

// Schedule task somewhere - ran nightly, or whenever

//

// This way you can refresh the status only on certain groups

// of data - for example, if the business flow means that once

// they become compliant, they can't go back, there is no need

// to refresh their status anymore

//

User::where('raw_status', '<>', 'compliant')->refreshStatus();

// Alternatively, the schedule could chunk results and do an update

// only to those not updated in the last 24 hours

//

$date = new DateTime;

$date->modify('-24 hours');

$formatted_date = $date->format('Y-m-d H:i:s');

User::where('last_updated', '>', $formatted_data)->refreshStatus();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值