我正在使用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
,则设置userID,但在添加到存储库后,将保存默认值。
我不明白为什么。 我也尝试使用以下代码手动持久,但没有解决方案:
/**
* @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');
}
我希望这个问题可以理解
最好的问候菲利克斯
答案 0 :(得分:3)
也许UserID名称不正确?如果您的数据库字段名为user_id
,则您的域名属性应为userId
。仅当您的数据库字段被调用user_i_d
时,它才应userID
。