Cphalcon: [BUG] Phalcon\Mvc\Model - serialize

Created on 21 Oct 2016  路  8Comments  路  Source: phalcon/cphalcon

Expected Behavior

group property of LdapUser should serializable from LdapUser and unserializable to LdapUser.

Actual Behavior

group property of LdapUser ignoring on serialize and unserialize.

Why I Needed This?

Users and Groups are entity in my project design. But I don't want to keep user group relationship as entity because this will not consistent by ldap changes. I want to keep group ids on own entity when user login. So I store user entity in session and session use serialization and group property of model orphan by serialization.

C:8:"LdapUser":74:{a:4:{s:2:"id";i:1;s:8:"username";s:6:"tugrul";s:4:"name";N;s:5:"email";N;}}
// CREATE TABLE `ldap_users` (
    // `id` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    // `username` VARCHAR(50) NOT NULL,
    // `name` VARCHAR(255) NOT NULL,
    // `email` VARCHAR(255) NOT NULL,
    // PRIMARY KEY (`id`),
    // UNIQUE INDEX `username` (`username`)
// )
// COLLATE='utf8_general_ci'
// ENGINE=InnoDB
// AUTO_INCREMENT=1

class LdapUser extends \Phalcon\Mvc\Model
{
    public $id;
    public $username;
    public $name;
    public $email;

    public $groups = [];

    public function getSource()
    {
        return 'ldap_users';
    }

    public function columnMap()
    {
        return [
            'id' => 'id',
            'name' => 'name',
            'username' => 'username',
            'email' => 'email'
        ];
    }

    public function initialize()
    {
        $this->skipAttributes(['groups']);
    }
}

$di = new \Phalcon\DI\FactoryDefault();
$di->set('db', function(){
    $connection = new \Phalcon\Db\Adapter\Pdo\Mysql([
        'host' => 'localhost',
        'username' => 'user',
        'password' => 'pass',
        'dbname' => 'project_db',
        'charset' => 'utf8'
    ]);
    return $connection;
});

$ldapUser = new LdapUser();
$ldapUser->setDi($di);
$ldapUser->id = 1;
$ldapUser->username = 'tugrul';
$ldapUser->groups[] = 2;
$ldapUser->groups[] = 8;
$ldapUser->groups[] = 10;

echo serialize($ldapUser);

Details

  • Phalcon version: 2.0.13
  • PHP Version: 5.6.26-0+deb8u1
  • Operating System: Debian 8.5
  • Installation type: Compiling from source
  • Zephir version (if any): 0.9.2a-dev
  • Server: Nginx + PHP-FPM
stale

Most helpful comment

This is not a design error. It's important when u serialize a database object to only serialize the database data and not some object junk... If you want to serialize your custom properties just overload serialize and unserialize methods because this is the right way to do it.

All 8 comments

It's not a bug, it's expected feature. It's ignored because doesn't exist on columnmap.

This is design error. What is the purpose of model instead of array result if there is no benefit in application logic like this?

Workaround.

It's dirty but useful to save time.

public function serialize()
{
        $properties = $this->toArray();
        $properties['groups'] = $this->groups;
        return serialize($properties);
}

This is not a design error. It's important when u serialize a database object to only serialize the database data and not some object junk... If you want to serialize your custom properties just overload serialize and unserialize methods because this is the right way to do it.

Purpose of model is to store database data obviously. Not some garbage stuff. If you want to store garbage stuff create seperateed class for this.

@makerlabs why is there a Phalcon\Mvc\Model::skipAttributes() method?

Common principal: Give back how did you take it.

Assert: assertTrue(serialize($model) === serialize(unserialize(serialize($model))))

There is an inconsistency

Why - because if you want to use mysql default value and/or don't allow any changes of columns in database perhaps ?

It has nothing about additional properties, they don't exist in insert/update even if you don't use skipAttributes.

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

Was this page helpful?
0 / 5 - 0 ratings