为什么学说总是以条件归零


Why is doctrine always returning null to a left join with condition?

我在Event和User实体(User.eventNotificationd)之间有一个多对多双向关联。

我正在寻找一个函数,该函数根据给定用户的id来引发此关联的事件。

所以我的函数是这样的:

public function getEventNotifiedById( $user, $eventId )
{
    //We only want to return users nearby who are available OR who
    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb->select( 'USER', 'EVENT' )
        ->from( 'Entity'User',  'USER' )
        ->where(
            $qb->expr()->eq( 'USER', ':user' )
        )
        ->leftJoin( 'USER.eventNotified', 'EVENT', 'WITH', 'EVENT.id = :eventId'  ); //CA NE MARCHE PAS chai pas pq
    $array = array(
         'user'     => $user,
         'eventId'  => $eventId
    );
    $qb->setParameters( $array );
    $user = $qb->getQuery()->getOneOrNullResult();
    if ( $user )
    {
        return $user->getEventNotified();
    }
    else
    {
        return false;
    }
}

但是$user->getEventNotificationd()总是返回null。为什么?

我想我在左边的Join中做错了什么,但我找不到什么。非常感谢

编辑:

以下是我的两个实体的必要部分:

用户实体:

<?php
namespace Entity;
use Doctrine'ORM'Mapping as ORM;
use Doctrine'Common'Collections'ArrayCollection;
use Doctrine'ORM'EntityRepository;
require_once 'Repositories/UserRepository.php';
/**
 * User
 *
 * @ORM'Table(name="user")
 * @ORM'Entity(repositoryClass="Repositories'UserRepository")
 */
class User
{
    /**
     * @var integer
     *
     * @ORM'Column(name="id", type="integer", nullable=false, unique=true)
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="IDENTITY")
     */
    private $id;
[...]

    /**
     * Bidirectional - Many users have notified they want to go to different events (OWNING SIDE)
     *
     * @var 'Doctrine'Common'Collections'Collection
     *
     * @ORM'ManyToMany(targetEntity="Entity'Event", inversedBy="userNotified", cascade={"persist"})
     */
    private $eventNotified;
[...]
}

事件实体

<?php
namespace Entity;
use Doctrine'ORM'Mapping as ORM;
use Doctrine'Common'Collections'ArrayCollection;
use Doctrine'ORM'EntityRepository;
require_once 'Repositories/EventRepository.php';
/**
 * User
 *
 * @ORM'Table(name="event")
 * @ORM'Entity(repositoryClass="Repositories'EventRepository")
 */
class Event
{
    /**
     * @var integer
     *
     * @ORM'Column(name="id", type="integer", nullable=false, unique=false)
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="IDENTITY")
     */
    private $id;
[...]

    /**
     * Bidirectional - Many users are intending many events (INVERSE SIDE)
     *
     * @ORM'ManyToMany(targetEntity="User", mappedBy="eventNotified", cascade={"remove"})
     */
    private $userNotified;
[...]
}

老实说,我在回购中经常使用这种关联,一切都很好。我已经尝试过var dump我的用户,并在左联接中不使用"with"子句的情况下看到他的事件通知,我可以在那里看到我的事件。

非常感谢您的帮助

[更新]

首先,您应该去掉require_one和use Doctrine''ORM''EntityRepository;您正在注释中设置存储库类,不应该使用实体中的任何存储库。此外,为什么在Event类中使用带id的unique=false?你还需要

@ORM'JoinTable(name="user_event")

许多

因此,假设你实际上在构造函数中将eventNotificationd设置为ArrayCollection,并在那里设置getter和setter,我很确定你会这样做

$this->eventNotified[] = $eventNotified;
return $this;

在你的getter中,你实际上是在返回

$this->eventNotified;

如果这一切都很好,而且我理解这一点,那么你的查询似乎无论如何都是错误的。你的加入需要在哪里之前。

$event = $qb->select( 'event' )
    ->from( 'Entity'event',  'event' )
    ->leftJoin( 'event.userNotified', 'user', 'WITH', 'user.id = :userId'  );
    ->where('event.id = :eventId')
    ->setParameters(['userId' => $user->getId(), 'eventId' => $eventId])
    ->getQuery()
    ->getOneOrNullResult();
 return $event ? $event : false;

您的多对多关系定义不正确,您必须指定要在拥有关系的站点上存储关系的附加交叉表。看一下条令文件。

class User
{
    // ...
    /**
     * Bidirectional - Many users have notified they want to go to different events (OWNING SIDE)
     *
     * @var 'Doctrine'Common'Collections'Collection
     *
     * @ORM'ManyToMany(targetEntity="Entity'Event", inversedBy="userNotified", cascade={"persist"})
     * @ORM'JoinTable(name="user_event")
     */
    private $eventNotified;
    // ...
}