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

时间:2016-12-05 22:46:07

标签: typo3 extbase typo3-6.2.x

我使用类Appointment创建了一个extbase扩展,其中包含属性expertises和另一个相同类型的subExpertises
这就是它们在Appointment类中的样子(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);
}

这是错误:

  

致命错误:在/var/www/typo3_src/typo3_src-6.2.25/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php中调用未定义的方法\ Domain \ Model \ Expertise :: getPosition()在第453行

现在似乎TYPO3认为Expertise属于ObjectStorage类型,因为它试图调用getPosition(),但我不知道为什么它会这样做而且< strong>我应该更改,以便使用新Appointment成功保存我的Expertises对象。

我尝试调试约会对象,但我找不到问题 - 对我来说似乎没关系,只是表明expertisessubExpertises已被修改。

1 个答案:

答案 0 :(得分:2)

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

如果您正在处理n:n-relation,则还需要在模型中将Property初始化为ObjectStorage并在TCA中对其进行配置。

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