Doctrine select entities with relation condition
<p>Given two tables:</p>

<p><strong>notification</strong></p>

<pre><code>id               (int PK)
title            (varchar(255))
date             (datetime)
</code></pre>

<p><strong>notification_seen</strong></p>

<pre><code>id               (int PK)
notification_id  (int FK)
user_id          (int FK)
</code></pre>

<p><strong>user</strong></p>

<pre><code>id               (int PK)
</code></pre>

<p>mapped as simple Doctrine entities where <strong>Notification</strong> has a relation defined as follows:</p>

<pre><code>@ORM\OneToMany(targetEntity="NotificationSeen", mappedBy="notification")
private $seenBy;
</code></pre>

<p>and <strong>NotificationSeen</strong>:</p>

<pre><code>@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;
</code></pre>

<p>and <strong>User</strong> entity:</p>

<pre><code>@ORM\OneToMany(targetEntity="NotificationSeen", mappedBy="users")
private $seen;
</code></pre>

<p>I want to select all <strong>Notification</strong> objects which are <strong>not</strong> connected through the <em>seenBy</em> relation.</p>

<p>How to select such entities in the NotificationRepository with the QueryBuilder? Any help would be appreciated.</p>

<p><strong>UPDATE:</strong></p>

<p>Current method implementation in the <strong>NotificationRepository</strong>:</p>

<pre><code>public function getUnreadNotification(User $user): array
{
    return $this-&gt;createQueryBuilder('u')
        -&gt;orderBy('u.date', 'desc')
        -&gt;getQuery()
        -&gt;getResult();
}
</code></pre>