<p>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.</p>

<p>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.</p>

<p>You can see the reason you're getting the error on <a href="https://php.net/manual/en/oop4.serialization.php" rel="noreferrer">this page</a>.</p>

CC BY-SA 3.0
2015-04-14T00:32:44.970Z
2015-04-14T00:32:44.970Z
Brad Bell
<p>I am storing a plugin model in session:</p>

<pre><code>$sessionModel = My_Model::populateModel([
    'token' =&gt; 'someData'
]);

craft()-&gt;httpSession-&gt;add('someKey', $sessionModel);
</code></pre>

<p>When I retrieve that later:</p>

<pre><code>$sessionModel = craft()-&gt;httpSession-&gt;get('someKey');

Craft::dd($sessionModel);
</code></pre>

<p>I get this:</p>

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

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

<plugin-development><session>
__PHP_Incomplete_Class retrieved from session
# __PHP_Incomplete_Class retrieved from session

(Asked by: username_0 on 2015-04-13)

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?

**Tags:** plugin-development, session


---

## 1 answers

### Answer by username_1 on 2015-04-14. Score: 5 | Accepted answer

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](https://php.net/manual/en/oop4.serialization.php).


---

