首页 文章

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

提问于
浏览
0

我使用 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() 但是我没有线索 why 它会这样做而 what I should change 为了用新的 Expertises 成功保存我的 Appointment 对象 .

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

1 回答

  • 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();
    }
    

相关问题