Cold_Class
Cold_Class

Reputation: 3494

Why is redirect resetting my object?

I made a frontend extension with Extbase in TYPO3 6.2 and while redirecting in my controller I'm loosing changes I've made to my object. I wonder if this is intended and why?

Here I see the change I've made to appointment in the var_dump.

/**
 *
 * @param Domain\Model\Appointment $appointment
 * @return void
 */
public function bookAction(Domain\Model\Appointment $appointment) {
    if ($appointment->getBooked()) {
        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($appointment);
        $this->redirect('update', null, null, array('appointment'=>$appointment));
    }
}

Then I see the original object before the changes I've made to appointment in the var_dump.
It seems like the passing of the changed appointment resets it back to its original state...?

/**
 * action update
 *
 * @param Domain\Model\Appointment $appointment
 * @return void
 */
public function updateAction(Domain\Model\Appointment $appointment) {
    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($appointment);
}

Upvotes: 0

Views: 746

Answers (2)

Viktor Livakivskyi
Viktor Livakivskyi

Reputation: 3228

Extbase controllers contain two methods of calling different action within your current action: redirect() and forward().

The difference is tiny, but consequences can be huge.

redirect() calls a different action via 30x HTTP redirect, so basically it requires complete page reload with restoring (and re-initializing) PHP session, data and objects.

Internally Extbase passes just an object's id to a second action, meaning, that in that second action your object is fetched from persistence again. And if the changes were not persisted in previous action, they'll be lost.

forward() just terminates the current MVC request and starts a new one without a page reload, meaning that all the session data and not-peristed changes are still available in a second action.

In this case Extbase passes not an id, but real object, so the changes are still there.

You can do one of the following:

  1. Use forward() instead of redirect().
  2. Persist changes to db via PersistenceManager before calling redirect().
  3. Preserve your object changes somehow (e.g. pass not a real instance to redirect(), but serialized string and then unserialize it in your second action).

Upvotes: 6

Georg Ringer
Georg Ringer

Reputation: 7939

I don't see any code where you actually persist anything. So you need that in your update action

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

Just changing the object without persisting it won't change anything!

Upvotes: 1

Related Questions