beastbytes / yii2-wizard

Yii2 Extension to handle multi-part form wizards
Other
37 stars 21 forks source link

Previous Event Model #2

Open zmoddynamics opened 9 years ago

zmoddynamics commented 9 years ago

Hi, thanks for writing this extension. I'm having trouble obtaining a previous event model. I want to obtain a previous event model during one of the following events.. I've tried using $event->sender->read(), but this is all Private Data constructs.. Is there an easier way to do this than writing the data to a file like in your example for the registration? I realize I can add the model to my own session variable during the registerWizardStep function, but I don't see the need to increase the session storage.. Is there an easier way?

beastbytes commented 9 years ago

Hi,

The step data is stored in the session as Array Objects - this is due to the way the Yii2 Session component works (see Sessions and Cookies in the Yii2 Guide, each step has an array of Array Objects to allow repetition of steps. So the resulting data layout in the session is

$session[StepDataKey][StepName][]

where the arrays are actually Array Objects; I wonder if your IDE is displaying the Array Objects as being private data constructs.

To access all step data within an event you can do

$stepData = $event->sender->read();

this returns the content of $session[StepDataKey]. Using the registration example, after the address step you will see something like:

$stepData = [
    'profile' => [
        0 => [
            'given_name' => 'John',
            'family_name' => 'Smith',
            'date_of_birth' => '1997-05-23',
            // _other private properties here
         ]
    ],
    [
        0 => [
            'street_address' => 'Stevenage Rd',
            'locality' => 'Fulham',
            'region' => 'London',
            'postal_code' => 'SW6 6HH',
            // _other private properties here
         ]
    ]
]

The arrays shown above are actually Array Objects, but they can be used in exactly the same way as ordinary arrays. So to access the a property of a model you do

$streetAddress = $stepData['address'][0]['street_address'];

If you only want data for a specific step you can do

$stepData = $event->sender->read('address');

Hope that helps.

newerton commented 8 years ago

@beastbytes create a wiki with this information.

beastbytes commented 8 years ago

Hi,

The read() method should do what you need; you can read back either all the steps or give the name of a step to read e.g. $event->sender->read('thisStep')

Steps are stored as PHP ArrayObjects; this allows steps to be repeated as needed. This is the case even if a step is only used once, so you will need to iterate over the object to get the model(s) stored in it. (see http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html for why ArrayObject is used).

Hope that helps.

On Sat, Sep 5, 2015 at 8:32 PM, zmoddynamics notifications@github.com wrote:

Hi, thanks for writing this extension. I'm having trouble obtaining a previous event model. I want to obtain a previous event model during one of the following events.. I've tried using $event->sender->read(), but this is all Private Data constructs.. Is there an easier way to do this than writing the data to a file like in your example for the registration? I realize I can add the model to my own session variable during the registerWizardStep function, but I don't see the need to increase the session storage.. Is there an easier way?

— Reply to this email directly or view it on GitHub https://github.com/beastbytes/yii2-wizard/issues/2.