lox / pheasant

A lightweight data mapper designed to take advantage of PHP 5.3+
http://getpheasant.com
MIT License
101 stars 21 forks source link

Adding virtual attribute support #77

Closed Jud closed 10 years ago

Jud commented 10 years ago

In some cases, it could be useful to have attributes that are not persisted to the DB. For instance, user models could have a virtual password property that is bcrypt'ed in a beforeSave handler, and stored in a column called encrypted_password.

We are using virtual attribute support together with onHydrate callbacks to normalize our data model as we migrate data to different tables, which allows us to keep model property access consistent, even across data migrations.

These changes allow you to define virtual properties in your schema, which makes sense if you think of the schema as the DomainObject schema, and not the mysql schema. So the property definitions become:

public funciton properties(){
  'products_id' => new Types\Integer(11, 'primary auto_increment'),
  'products_volume_price' => new Types\Decimal(15, 2),
  # This property is set onHydrate by getting the latest `rate` from the products_rate table
  'rate' => new Types\Decimal(15, 2, 'virtual'),
}

If you are interested in this pull, I'll add tests.

mtibben commented 10 years ago

There are better ways to approach this. Using your password example

    class User 
    {
        public function properties() 
        {
            return array('pwhash' => new Types\String());
        }

        public function setPassword($password) 
        {
            $this->pwhash = password_hash($password);
        }
       ...
Jud commented 10 years ago

@mtibben - man, your right. I swear I tried creating a setter and it didn't work. I might work on a few doc fixes so this stuff becomes more obvious.

Jud commented 10 years ago

@mtibben actually I just tried implementing this with custom getters/setters and I get a 'No getter available for $x' error.

If you were suggesting that I would be able to do User->password = 'xyz', I don't believe that is the case. If you were suggesting to use User->setPassword('xyz') I think pheasant could do better than that.

Jud commented 10 years ago

@lox is there a way to do custom magic getters and setters in pheasant right now?

Jud commented 10 years ago

I can accomplish what I want with a simple onHydrate and public property! #81 fixes this.