Php 原则选择具有关系条件的实体

Php 原则选择具有关系条件的实体,php,symfony,doctrine-orm,doctrine,Php,Symfony,Doctrine Orm,Doctrine,给出两个表格: 通知 id (int PK) title (varchar(255)) date (datetime) id (int PK) notification_id (int FK) user_id (int FK) 已看到通知 id (int PK) title (varchar(255)) date

给出两个表格:

通知

id               (int PK)
title            (varchar(255))
date             (datetime)
id               (int PK)
notification_id  (int FK)
user_id          (int FK)
已看到通知

id               (int PK)
title            (varchar(255))
date             (datetime)
id               (int PK)
notification_id  (int FK)
user_id          (int FK)
用户

id               (int PK)
映射为简单条令实体,其中通知具有如下定义的关系:

@ORM\OneToMany(targetEntity="NotificationSeen", mappedBy="notification")
private $seenBy;
通知见

@ORM\ManyToOne(targetEntity="Notification", inversedBy="seenBy")
@ORM\JoinColumn(name="notification_id")
private $notification;

@ORM\ManyToOne(targetEntity="User", inversedBy="seen")
@ORM\JoinColumn(name="user_id")
private $user;
public function getUnreadNotification(User $user): array
{
    return $this->createQueryBuilder('u')
        ->orderBy('u.date', 'desc')
        ->getQuery()
        ->getResult();
}
用户实体:

@ORM\OneToMany(targetEntity="NotificationSeen", mappedBy="users")
private $seen;
我想选择所有通过seenBy关系连接的通知对象

如何使用QueryBuilder在NotificationRepository中选择此类实体?任何帮助都将不胜感激

更新:

NotificationRepository中的当前方法实现:

@ORM\ManyToOne(targetEntity="Notification", inversedBy="seenBy")
@ORM\JoinColumn(name="notification_id")
private $notification;

@ORM\ManyToOne(targetEntity="User", inversedBy="seen")
@ORM\JoinColumn(name="user_id")
private $user;
public function getUnreadNotification(User $user): array
{
    return $this->createQueryBuilder('u')
        ->orderBy('u.date', 'desc')
        ->getQuery()
        ->getResult();
}

基本上,您只需要查询数据库中没有相关通知的
通知

public function getUnreadNotification(User $user): array
{
    return $this->createQueryBuilder('n')
        ->join('n.seenBy', 'ns')
        ->where('ns.id IS NULL')
        ->orderBy('n.created', 'desc')
        ->getQuery()
        ->getResult();
}

以下是我的建议,请迈克发表评论

[…]也就是说,我需要从通知表中选择指定用户的通知表中不存在的所有实体

我没有试过,但它应该有用

public function getUnreadNotifications(User $user)
{
    // Create query builder
    $qb = $this->createQueryBuilder('n');

    // someone saw the notification
    $nobodySawIt = $qb->expr()->eq('SIZE(n.seenBy)', 0);

    // Create expression for 'another guy saw the notification, but not my user'
    $someOneElseSawIt = $qb->expr()->andX(
        $qb->expr()->neq('SIZE(n.seenBy)', 0), // someone saw the notification
        $qb->expr()->neq('u.id', $user->getId()) // but not this user
    );

    // Create the query
    $qb
        ->leftJoin('n.seenBy', 's') // Join on NotificationSeen
        ->leftJoin('s.user', 'u') // Join on User
        ->orWhere($nobodySawIt) // I want unread notifications
        ->orWhere($someOneElseSawIt); // and notifications that this guys didn't read yet
      /*
        ->addOrderBy(alias.property)
        ->setFirstResult($offset)
        ->setMaxResults($limit);
       */

    return $qb->getQuery()->getResult();
}

告诉我是否有问题

如果在用户和通知之间没有看到通知,您如何找到通知?在用户和通知之间是否有其他未在您的帖子中公开的链接?或者您的意思是要创建新的notificationSeen关系?我想通过通知存储库而不是用户实体来选择这些实体。也就是说,我需要从通知表中选择指定用户的通知表中不存在的所有实体。我们可以看看您尝试的代码吗?查询生成器部分?我添加了我得到的,基本上什么都没有。通知与用户没有直接关系,除非通过NotificationSeen。如果NotificationSeen连接其他两个表,您希望如何在db中实现连接?“错误:无法添加对非结果变量的条件”我更新了答案,请仔细检查您使用的是我最新的代码。好的,请看这里。将
ns为NULL
更改为
ns.id为NULL
。谢谢。我只是想澄清一下,我想要的东西叫做RIGHT JOIN。