Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Adding virtual attribute support #77

Closed
wants to merge 1 commit into from
Closed

Adding virtual attribute support #77

wants to merge 1 commit into from

Conversation

Jud
Copy link
Collaborator

@Jud Jud commented Sep 24, 2013

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
Copy link
Collaborator

mtibben commented Sep 25, 2013

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
Copy link
Collaborator Author

Jud commented Sep 25, 2013

@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 Jud closed this Sep 25, 2013
@Jud Jud deleted the add-virtual-attribute-support branch September 25, 2013 13:18
@Jud
Copy link
Collaborator Author

Jud commented Sep 26, 2013

@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 Jud restored the add-virtual-attribute-support branch September 26, 2013 20:36
@Jud Jud reopened this Sep 26, 2013
@Jud
Copy link
Collaborator Author

Jud commented Sep 26, 2013

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

@Jud
Copy link
Collaborator Author

Jud commented Sep 27, 2013

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

@Jud Jud closed this Sep 27, 2013
@Jud Jud deleted the add-virtual-attribute-support branch March 28, 2014 02:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants