1

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?

John Foley
  • 4,373
  • 3
  • 21
  • 23

1 Answers1

0

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();
Erick Engelhardt
  • 704
  • 2
  • 10
  • 30
  • I've looked at what gets stored in the session by tailing the session file and have noticed when an object gets saved in a phalcon session it only stores the toArray() data as you provided above, not the serialized object. The reason its nice to store it this way is because when you "get" it from the session phalcon hydrates the associated model with the array data. And no, it does not save the new mapped name - have you seen the points I made about column maps in my question? – John Foley Mar 31 '15 at 21:10