Phalcon models clobbered in session

111 Views Asked by At

I am storing a model in my session. I am using column maps in my model so that if the column name changes in the database I don't have to make changes throughout my application. So if 'firstName' changed to 'first' in the database i could keep referencing 'firstName' throughout the application. In my testing I have found that unless the column map key and value are equal the property will always be stored as null in the session.

This will work:

/**
 * Independent Column Mapping.
 *
 * Keys reference property in database
 * Values reference property application-wide
 */

// db column name = 'firstName'
public static function columnMap()
{
    return
    [
        'firstName' => 'firstName'
    ];
}

This will work:

// db column name = 'first'
public static function columnMap()
{
    return
    [
        'first' => 'first'
    ];
}

This will fail:

// db column name = 'first'
public static function columnMap()
{
    return
    [
        'first' => 'firstName'
    ];
}

I don't foresee changing database column names, nor do I like the inconsistency of referencing a property by a different name in the database and in the application. However, I would like to remain flexible and make sure this works in case I run into a scenario where I need this change.

Does anyone know why the session clobbers the property value when the column map key-value pair differs?

1

There are 1 best solutions below

1
On

Why are you saving the object in session? This will consume a lot of memory. It's better to save only an array. And it will save the new mapped name.

$user = User::findFirstFromId(1000);
$this->session->set('user') = $user->toArray();