Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Simpleauht's profile field serialization wierdness
  • Some strange things around reading user profile fields from Database. I have this function in my user model:
     public function get_userfields()
     {
      $data = unserialize($this->profile_fields) ?: array();
      return $data
     }
    

    In my view I call this function like this and I get two different results:
    foreach ($comments as $comment)
         var_dump( $comment->user->get_userfields());
    

    a) if user X is not logged in then user->get_userfields() returns array of profile fields as expected
    b) if user X is signed-in then unserialize function throws this error: ErrorException [ Notice ]: unserialize() [function.unserialize]: Error at offset 5 of 105 bytes I am passing data to view like this:
    $comments = Model_Comment::find()
        ->related('user')
        ->order_by('created_at', 'desc')
        ->limit(12)
        ->get();
    $view->set('comments', $comments, false);
    

    I am clueless of what is happening here and it getting me crazy. Can anyone tell me what I am doing wrong here?
  • You can not pass serialized data to your view. FuelPHP's security system will escape it, which makes it no longer valid. Either pass it to the view without escaping (not very secure), unserialize in your controller before passing the object to the view, or use the Type observer in your model to automatically do the serialize/unserialize, and have it always available as an array. See https://github.com/fuel/depot/blob/1.0/develop/fuel/modules/admin/classes/model/user.php as an example of a user model.
  • Harro Verton wrote on Sunday 11th of March 2012:
    You can not pass serialized data to your view. FuelPHP's security system will escape it, which makes it no longer valid. Either pass it to the view without escaping (not very secure),

    I know that, that's why I have pass data to view via set($name, $value, false) as described here: http://docs.fuelphp.com/general/views.html. But that did not help. Again the real weirdness is why does it work for user Y and for all users who are not signed in, and why does it fail for user who is signed-in ?
    Harro Verton wrote on Sunday 11th of March 2012:
    or use the Type observer in your model to automatically do the serialize/unserialize, and have it always available as an array. See https://github.com/fuel/depot/blob/1.0/develop/fuel/modules/admin/classes/model/user.php as an example of a user model.

    Thanks I will try implementing it this way. Will the default simpleauth class know how to work with this (especiali the create and update user methods)?
  • Harro Verton wrote on Sunday 11th of March 2012:

    Ok I have tried this but it doesn't work. My user model:
     protected static $_properties = array(
      'id',
      'username',
      'password',
      'group',
      'email',
      'last_login',
      'login_hash',
      'remember_me',
      'profile_fields' => array(
       'data_type' => 'serialize',
      ),
      'created_at',
      'updated_at'
     );
    

    var_dump($this->current_user->profile_fields); will still return unserialized string... Am I missing something? UPDATE: I was missing observer definition, it works fine now. Thanks for helping!!
  • No, as SimpleAuth doesn't use models, it uses direct DB calls. Do a \Debug::dump() in your view to see what is different about the profile_fields value for logged in users, otherwise you keep guessing as to what the issue might be.
  • UPDATE: I was missing observer definition, it works fine now. Thanks for helping!!
  • I am having the same problem, but the links you mentioned is not working
    Can you help me out?
  • is the data returned by the tables relation, any idea?
  • Simpleauth only uses a single user table, and standard DB calls. So no relation (or even a second table) is used.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion