数据为空时的安全写法。

This commit is contained in:
Dev
2026-10-08 16:38:37 +08:00
parent 107cbbe971
commit 0843996469
@@ -29,10 +29,14 @@ class Message extends Api
}
//总未读数(兼容旧字段)+ 分类未读数(前端tab角标用)
$unreadCount = MessageService::where($where + ['is_read' => CommonService::STATUS_OFF])->count();
//where() 走的是 Query 对象,select() 返回普通数组而非 Collection,需兼容处理
$unreadList = MessageService::where(['to_user_id' => $user_id, 'is_read' => CommonService::STATUS_OFF])
->field('message_type, count(*) as total')
->group('message_type')
->select()->toArray();
->select();
$unreadList = is_object($unreadList) && method_exists($unreadList, 'toArray')
? $unreadList->toArray()
: (array)$unreadList;
$unreadMap = [
MessageService::TYPE_SYSTEM => 0,
MessageService::TYPE_NOTICE => 0,