调用未定义的方法 getPosition() - 为什么和修复

Call to undefined method getPosition() - Why and Fix

我用一个 class Appointment 和一个 属性 expertises 和另一个相同类型的 subExpertises 做了一个 extbase 扩展。
这是他们在 Appointment class 中的样子(subExpertises 是一样的):

    /**
     * expertises
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<...\Domain\Model\Expertise>
     */
    protected $expertises = NULL;

    /**
     * Adds an expertise
     *
     * @param ...\Domain\Model\Expertise $expertise
     * @return void
     */
    public function addExpertise(...\Domain\Model\Expertise $expertise) {
        $this->expertises->attach($expertise);
    }

以流畅的形式编辑约会后,在我的控制器中执行此代码时出现错误:

/**
 *
 * @param \Domain\Model\Appointment $appointment
 * @return void
 */
public function bookAction(\Domain\Model\Appointment $appointment) {

    //empty all expertises of appointment - then fill them with the selected from lawyer
    $appointment->setExpertises(new \TYPO3\CMS\Extbase\Persistence\ObjectStorage());
    $appointment->setSubExpertises(new \TYPO3\CMS\Extbase\Persistence\ObjectStorage());

    //add all checked expertises of lawyer to appointment
    foreach ($appointment->getLawyer()->getExpertises() as $expertise) {
        if ($expertise->getChecked()) {
            $appointment->addExpertise($expertise);
        }
        foreach ($expertise->getSubExpertises() as $subExpertise) {
            if ($subExpertise->getChecked()) {
                $appointment->addSubExpertise($subExpertise);
            }
        }
    }
    $this->appointmentRepository->update($appointment);
}

这是错误:

Fatal error: Call to undefined method \Domain\Model\Expertise::getPosition() in /var/www/typo3_src/typo3_src-6.2.25/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php on line 453

现在 TYPO3 似乎认为 Expertise 是类型 ObjectStorage 因为它试图调用 getPosition() 但我不知道 为什么 它做到了这一点,我应该改变什么,以便用新的 Expertises 成功保存我的 Appointment 对象。

我试过调试约会对象,但我找不到问题 - 我觉得没问题,它只是显示 expertisessubExpertises 已被修改。

Getter Extbase 中的方法并不神奇,您必须明确定义它们。

如果您正在处理 n:n 关系,您还需要在模型中将 属性 初始化为 ObjectStorage 并在 TCA 中对其进行配置。

/**
 * Initialize all ObjectStorage properties.
 *
 * @return void
 */
protected function initStorageObjects() {
    $this->yourProperty = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}