Doctrine ArrayCollection 匹配条件函数导致 Undefined 属性: MyEntity::$1 (Symfony 3.4)

Doctrine ArrayCollection matching criteria function results in Undefined property: MyEntity::$1 (Symfony 3.4)

我将实体 "TrainingProgressEntry" 定义为 @ORM\Entity 和 "training" 属性,如下所示:

/**
 * @ORM\ManyToOne(targetEntity="Training", inversedBy="training_progress")
 * @ORM\JoinColumn(name="training_id", referencedColumnName="id")
 */
protected $training;

匹配@ORM\Entity"Training"定义了一个属性"training_progress"like

/**
 * @ORM\OneToMany(targetEntity="TrainingProgressEntry", mappedBy="training", cascade={"remove"})
 * @ORM\OrderBy({"entry_date" = "ASC"})
 */
protected $training_progress;

和一个 getter 方法,像这样

/**
 * Get trainingProgress
 *
 * @return \Doctrine\Common\Collections\ArrayCollection
 */
public function getTrainingProgress()
{
    return $this->training_progress;
}

最后,我定义了一个 getter 方法,旨在 return 只有日期比某个参考日期更新的条目:

/**
 * @return \Doctrine\Common\Collections\ArrayCollection
 */
public function getTrainingProgressSinceStart()
{
    $startTime = $this->getUser()->getStart();
    $criteria = Criteria::create()
        ->andWhere(Criteria::expr()->gt('entry_date', $startTime))
        ->orderBy(['entry_date', 'ASC']);
    return $this->getTrainingProgress()->matching($criteria);
}

使用最后一个函数时,我得到以下结果 "ContextErrorException":

Notice: Undefined property: AppBundle\Entity\TrainingProgressEntry::

来自

vendor\doctrine\collections\lib\Doctrine\Common\Collections\Expr\ClosureExpressionVisitor.php

尝试 "return $object->$field".

trace显示是上述函数"getTrainingProgressSinceStart()"在

行引起的
return $this->getTrainingProgress()->matching($criteria);

由于某种原因,匹配功能似乎无法被识别或... 我真的不知道现在要找什么。 非常欢迎任何提示。

您可能已经解决了这个问题,但我会以任何一种方式回答以供其他人参考。

方法:orderBy of criteria 接受一个数组,Key 是字段,排序顺序是值,所以你有:

/**
 * @return \Doctrine\Common\Collections\ArrayCollection
 */
public function getTrainingProgressSinceStart()
{
    $startTime = $this->getUser()->getStart();
    $criteria = Criteria::create()
        ->andWhere(Criteria::expr()->gt('entry_date', $startTime))
        ->orderBy(['entry_date', 'ASC']);
    return $this->getTrainingProgress()->matching($criteria);
}

确实应该是 ['entry_date' => 'ASC']:

/**
 * @return \Doctrine\Common\Collections\ArrayCollection
 */
public function getTrainingProgressSinceStart()
{
    $startTime = $this->getUser()->getStart();
    $criteria = Criteria::create()
        ->andWhere(Criteria::expr()->gt('entry_date', $startTime))
        ->orderBy(['entry_date' => 'ASC']);
    return $this->getTrainingProgress()->matching($criteria);
}

来源:https://github.com/doctrine/collections/blob/c23e14f69b6d2d1d1e389bc8868500efc447af7b/lib/Doctrine/Common/Collections/Criteria.php#L152