Rafael Adel
Rafael Adel

Reputation: 7759

Symfony - Filtering join causes the query to return nothing

I'm trying to perform a query that returns the events that're approved and not booked by the current user.

When i perform this query I get no events returned where it should return on event which is not booked by the current user.

 public function getAvailableEvents(UserInterface $user)
    {
        $q = $this->getEntityManager()->createQuery('
                    select v from Tsk\FEBundle\Entity\Event v
                    left join v.attendance a where a.id != :user
                    and v.is_approved = true
                ');
        $q->setParameter("user", $user);
        return $q->execute();
    }

Here's my two entities User and Event specific part for this relation.

User :

/**
 * @ORM\ManyToMany(targetEntity="Event", inversedBy="attendance", cascade={"persist", "remove"})
 * @ORM\JoinTable(name="users_events",
 *      joinColumns={@ORM\JoinColumn(name="event_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
 *      )
 */
 protected $booked_events;

Event entity:

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="booked_events")
 */
 protected $attendance;

Upvotes: 1

Views: 50

Answers (1)

VisioN
VisioN

Reputation: 145368

As discussed, the problem appears to be in a query. If you want to get events which are not attended by anyone, you may use WHERE a IS NULL condition.

Upvotes: 1

Related Questions