'int', 'from_user_id' => 'int', 'to_user_id' => 'int', 'content' => 'string', 'is_read' => 'int', 'read_time' => 'int', 'create_time' => 'int', 'update_time' => 'int', 'delete_time' => 'int', ]; // 自动时间戳 protected $autoWriteTimestamp = true; // 软删除 protected $deleteTime = 'delete_time'; /** * 获取是否已读文本 */ public function getIsReadTextAttr() { $statuses = [ 0 => '未读', 1 => '已读', ]; return $statuses[$this->is_read] ?? '未知状态'; } /** * 标记为已读 */ public function markAsRead() { if ($this->is_read == 0) { $this->is_read = 1; $this->read_time = time(); $this->save(); } return $this; } /** * 关联发送者用户 */ public function fromUser() { return $this->belongsTo('app\common\model\User', 'from_user_id'); } /** * 关联接收者用户 */ public function toUser() { return $this->belongsTo('app\common\model\User', 'to_user_id'); } /** * 判断是否为系统消息 */ public function isSystemMessage() { return $this->from_user_id == 0; } }