学说OneToMany集合

时间:2019-03-01 15:28:35

标签: symfony doctrine

你好,我试图按多个字段命令收集学说

尝试了这样的事情

  /**
     * @var Collection
     * @ORM\OrderBy({"date" = "ASC","TimeBegin" = "ASC"})
     * @ORM\OneToMany(targetEntity="Schedule", mappedBy="event")
     */
    protected $schedules;

此代码不起作用

日期字段的格式为“ 1927-12-01” timeBegin“ 00:13:01”

这是我的查询

 public function getAppointmentDetails(int $eventId): ?Event
    {

        $eventAlias = 'event';
        /** @var EventQueryBuilder $queryBuilder */
        $queryBuilder = $this->createQueryBuilder($eventAlias);

        $queryBuilder->select($eventAlias)
            ->whereEventId($eventId)
            ->withRoom()
            ->withService()
            ->withSchedulesAndInstructorsOrderedByDateAndTime();


        $appointmentDetails = $queryBuilder->getQuery()->getOneOrNullResult();

        return $appointmentDetails;
    }

和我的SchedulesAndInstructorsOrderedByDateAndTime方法

 /**
     * With Schedules And Instructors Ordered by Date and Time
     * @return EventQueryBuilder
     */
    public function withSchedulesAndInstructorsOrderedByDateAndTime() : EventQueryBuilder
    {
        $this->join($this->getRootAliases()[0] . '.schedules', 'sc');
        $this->join('sc' . '.instructors', 'in');

        return $this;
    }

问题是,如果我添加orderBy,我的讲师集合将为空

1 个答案:

答案 0 :(得分:0)

文档说明:

  

为保持对数据库的影响较小,仅当在DQL查询中对集合进行联接时,才会将这些隐式ORDER BY项添加到DQL查询中。

因此,您需要通过将Schedule添加到您的选择中来进行访存联接:

    /**
     * With Schedules And Instructors Ordered by Date and Time
     * @return EventQueryBuilder
     */
    public function withSchedulesAndInstructorsOrderedByDateAndTime() : EventQueryBuilder
    {
        $this->addSelect('sc');
        $this->join($this->getRootAliases()[0] . '.schedules', 'sc');
        $this->join('sc' . '.instructors', 'in');

        return $this;
    }