Mikael
Mikael

Reputation: 1319

TYPO3 repository->add not works

I have TYPO3 version 7.6.18

$newMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Istar\\Femessages\\Domain\\Model\\Message');
                $newMessage->setUserFrom($myUid);
                $newMessage->setUserTo($userTo);
                $newMessage->setChainUid($messageChainUid);
                $newMessage->setMessage($message);
                $this->messageRepository->add($newMessage);
                $persistenceManager = $this->objectManager->get("TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager");
                $persistenceManager->persistAll();

Repository does't add new to DB without

            $persistenceManager = $this->objectManager->get("TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager");
            $persistenceManager->persistAll();

Tell me please, why ?

Upvotes: 2

Views: 3828

Answers (1)

Euli
Euli

Reputation: 1143

The persistence manager of Extbase kicks in, when the request is being finished and your action is done with rendering the view and answering the request. This is by design and why the repository just stores that it has to update or insert the object you are giving to it instead of actually doing it.

This approach has to do with lazy loading, where you just load data, when you really need it. Have a look at your objects, the repository sends you back, when you request them. If you inspect the result with a debugger like xDebug, you will notice that the result actually does not contain any of your requested objects, it is just a representation of your Query. The data will be fetched only if you need it, say if you call toArray on this QueryResult object.

The same is done for data persistence. The system assumes that you don't need the data persisted right away, so it waits with sending the data to the database until all processing of the request has been done. This way the data can be included with less database connections and this in return will save time.

If you really need to persist objects after you added them, you need to call the PersistenceManager like you described. This could be, if you want to generate a link to your new, not yet existing, object.

Keep in mind that the persistAll Method does what it says, it will persist ALL changes to ALL objects given to ALL repositories until that point of execution. There is no way of persisting just one specific object, at least up until now, that I know of.

Upvotes: 3

Related Questions