1

I am storing a plugin model in session:

$sessionModel = My_Model::populateModel([
    'token' => 'someData'
]);

craft()->httpSession->add('someKey', $sessionModel);

When I retrieve that later:

$sessionModel = craft()->httpSession->get('someKey');

Craft::dd($sessionModel);

I get this:

__PHP_Incomplete_Class#1
(
    [__PHP_Incomplete_Class_Name] => 'Craft\\My_Model'
    [*:classSuffix] => 'Model'
    [*:strictAttributes] => true
    [Craft\BaseModel:_classHandle] => null
    [Craft\BaseModel:_attributeConfigs] => array
    (
        'token' => array
        (
            'type' => 'string'
            'required' => true
        )
    )
    [Craft\BaseModel:_attributes] => array
    (
        'token' => array
        (
            'token' => 'someData'
        )
    )
    [Craft\BaseModel:_extraAttributeNames] => null
    [CModel:_errors] => array()
    [CModel:_validators] => null
    [CModel:_scenario] => ''
    [CComponent:_e] => null
    [CComponent:_m] => null
) 

Is there a better way to store a model in session? Should it be serialised and unserialised in some way?

5

Save yourself the headache and don't store an entire Model in session. More to the point, don't store any complex objects in session.

It's much better practice to store just the information you need in session to create the model when it's needed (i.e. some unique ID). You won't run into issues like you're seeing and you won't be using unnecessary server resources serializing and unserializing these complex objects.

You can see the reason you're getting the error on this page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.