0

我正在使用 TYPO3 7.6.10 并构建了我的第一个扩展。

我想在我的控制器的 createAction 函数中为我的对象添加一个属性。但是修改没有保存。

这是我的代码:

/**
 * action create
 *
 * @param \Typo3\LpSurvey\Domain\Model\Sigil $newSigil
 * @param array $answers
 * @internal param Survey $newSurvey
 */
public function createAction(Sigil $newSigil, Array $answers)
{
    $newSurvey = $this->objectManager->get('Typo3\LpSurvey\Domain\Model\Survey');
    $this->userID = $GLOBALS['TSFE']->fe_user->user['uid'];

    //this modifications are saved
    foreach ($answers as $key => $answer) {
        $newSurveyItem = $this->objectManager->get('Typo3\LpSurvey\Domain\Model\SurveyItem');

        $newSurveyItem->setQuestionId($key);
        $newSurveyItem->setValue($answer);

        $newSurvey->addAnswer($newSurveyItem);
    }


    //BUT this modification is not saved
    $newSigil->setUserID($this->userID);

    $newSigil->setSurvey($newSurvey);

    $this->sigilRepository->add($newSigil);

    $this->redirect('list');
}

如果我调试我的对象$newSigil,则设置了用户 ID,但在添加到存储库后,默认值将被保存。

我不明白为什么。我也尝试使用以下代码手动坚持,但没有解决方案:

/**
 * @var \typo3\CMS\Extbase\Persistence\Generic\PersistenceManager
 * @inject
 */
protected $persistenceManager;

public function createAction(Sigil $newSigil, Array $answers)
{
    $newSurvey = $this->objectManager->get('Typo3\LpSurvey\Domain\Model\Survey');
    $this->userID = $GLOBALS['TSFE']->fe_user->user['uid'];


    foreach ($answers as $key => $answer) {
        $newSurveyItem = $this->objectManager->get('Typo3\LpSurvey\Domain\Model\SurveyItem');

        $newSurveyItem->setQuestionId($key);
        $newSurveyItem->setValue($answer);

        $newSurvey->addAnswer($newSurveyItem);
    }



    $newSigil->setUserID($this->userID);
    $newSigil->setSurvey($newSurvey);

    $this->persistenceManager->persistAll();

    $this->sigilRepository->add($newSigil);

    $this->redirect('list');
}

我希望这个问题可以理解

最好的问候菲利克斯

4

1 回答 1

3

也许 UserID 的名称不正确?如果你的数据库字段被称为user_id你的属性域应该userId。只有当您的数据库字段被调用user_i_d时,它才应该userID

于 2016-10-11T10:18:05.113 回答