You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用maximilienGilet/notification-bundle的mgilet_notification_count()触发致命错误

解决Mgilet NotificationBundle的mgilet_notification_count()报错问题

这个错误的核心原因很明确:mgilet_notification_count()函数必须接收一个实现了NotifiableInterface的用户实例作为参数,你调用时没有传参,所以触发了类型错误。下面是分步解决方案:

1. 先确认你的用户实体符合要求

首先,你的User类必须实现bundle要求的NotifiableInterface,并引入对应的Trait来实现接口方法。修改你的User实体代码:

namespace App\Entity;

use Mgilet\NotificationBundle\Entity\NotifiableInterface;
use Mgilet\NotificationBundle\Entity\NotifiableTrait;
// 其他必要的use语句...

class User implements NotifiableInterface
{
    // 引入Trait来实现接口的默认方法
    use NotifiableTrait;

    // 你的用户实体原有代码(比如id、email、密码等字段和方法)
}

2. 确认Bundle配置正确

确保你在Symfony的配置文件(比如config/packages/mgilet_notification.yaml)中指定了正确的用户类路径:

mgilet_notification:
    user_class: App\Entity\User # 替换成你实际的User类命名空间

3. 在模板中正确调用函数

根据你的使用场景,选择对应的传参方式:

  • 场景1:统计当前登录用户的未读通知数
    如果你是在已登录用户的模板中使用,直接传入Symfony内置的app.user变量即可:
    {{ mgilet_notification_count(app.user) }}
    
  • 场景2:统计特定用户的通知数
    如果你需要统计某个指定用户的通知数,先在控制器中把用户实例传递到模板:
    // 控制器示例
    public function someAction()
    {
        $targetUser = $this->getDoctrine()->getRepository(User::class)->find(1);
        return $this->render('your_template.html.twig', [
            'targetUser' => $targetUser
        ]);
    }
    
    然后在模板中调用:
    {{ mgilet_notification_count(targetUser) }}
    

完成以上步骤后,再刷新页面,这个错误应该就会消失了。

内容的提问来源于stack exchange,提问作者Fazhom Arnaud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:51:59