Tillebeck
Tillebeck

Reputation: 3523

TYPO3 extbase - get "uid" of non persisted object

Just started out with the frst extbase extension. In localconf I added these actions: 'list, new, show, create, edit'.

Default was that "create" redirected to "list" with no arguments, and that worked fine right after creation of the extension.

$this->redirect('list'); // <- this works if used

But instead of redirecting to "list" I would like to redirect to "show" to display the newly added priceFormCalc. A collegue helped me to acomplish this using persist.

Below is the code and it works. But reading on the net it seem that it should not be best practice to run persist. It should be doable to call the action show without manually persisting first.

So question is: Is this the correct way to do it, or is there a more "ext base" way show a newly created record?

public function createAction(\TYPO3\OgNovomatrixpricecalc\Domain\Model\PriceCalcForm $newPriceCalcForm) {
    $this->priceCalcFormRepository->add($newPriceCalcForm);
    $this->flashMessageContainer->add('Your new PriceCalcForm was created.');

    // workaround - or the right way?
    $persistenceManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Tx_Extbase_Persistence_Manager');
    $persistenceManager->persistAll(); // <- save i database
    $uid = $newPriceCalcForm->getUid(); // <- get UID of newly saved record

    // do redirect using uid of newly created priceCalcForm
    $this->redirect('show',Null,Null, array('priceCalcForm' => $uid));
}

To

Upvotes: 2

Views: 4090

Answers (1)

lorenz
lorenz

Reputation: 4558

You can persist the current state and then get the uid. Inject the configuration manager (TYPO3 6.x way):

/**
 * persistence manager
 *
 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
 * @inject
 */
protected $persistenceManager;

Then use

$this->persistenceManager->persistAll();

to apply all changes. Then you can pass your object (or only the uid) to the action.

Upvotes: 3

Related Questions