Felix Felix - 1 month ago 10
PHP Question

Extbase: Modified object does not saved in the repository correctly

I am using Typo3 7.6.10 and I build my first extension.

I want to add a property to my object in the createAction function of my controller.
But the modifications are nt saved.

Here is my code:

/**
* 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');
}


If I debug my object
$newSigil
the userID is set, but after adding to the repository the default value will be saved.

I dont understand why.
I also try to persist manually with following code, but no solution:

/**
* @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');
}


I hope the question is understandable

Best regards Felix

Answer

Maybe UserID is not correct named? If your database field is called user_id your property for the domain should userId. Only if your database field is called user_i_d it should userID.

Comments