开发者

How do I set a custom mutator with Doctrine YAML Schema?

I'm using this tutorial as the basis for a Code Igniter / Doctrine based project.

http://www.phpandstuff.com/articles/codeigniter-doctrine-scratch-day-3-user-signup-form

Instead of using the code as-is for the models, my project is using a YAML schema file to generate the models

I've hit a snag, in that I've got no idea how to represent the following using YAML:

$this->hasMutator('password', '_encrypt_password');

(this is from the User model code, under 'Adding Mutators') Specifically, I'm having trouble with the $this->hasMutator line

I've googled until blue, searched documentation for Doctrine, symfony (which I know uses 开发者_Go百科Doctrine heavily) and even grep'd the codebase for references to mutators - I've come up empty

Is there a way to represent the line $this->hasMutator('password', '_encrypt_password'); using Doctrine YAML?


(Just a note to clarify: we are talking about Doctrine 1.x here and not Doctrine 2.x) No, there is no a way to define mutators directly in your YAML schema. Are you sure you must register the mutator there?

You could get around this limitation by creating you own doctrine behavior. Doctrine behaviors can be assigned to your models in the YAML schema. Read more here:

http://www.doctrine-project.org/projects/orm/1.2/docs/manual/behaviors/pl

In your case the behavior would look something like this:

class EbonhandsTemplate extends Doctrine_Template
{
    public function setUp()
    {
        $this->hasMutator('password', '_encrypt_password');
    }

    public function _encrypt_password
    ....
}

And in your yaml schema:

EbonhandsModel:
  actAs: [EbonhandsTemplate]


I ended up solving this by adding a new method to the generated User.php model as follows:

public function setPassword($pass)
{
  $this->_set('password', $this->_encrypt_password($pass));
}

It achieves the same end result as the tutorial linked above (e.g. allowing modifications to the User model schema via YAML without losing code), but doesn't feel as elegant or general-purpose.

I'm marking Olof's answer as correct/accepted, as his solution feels more extensible and OO - mine "smells" a bit


There is one important thing to notice about mutators if you loading data from YAML files.

As in tutorial you mentioned if your User class is like:

class User extends ModelBaseUser
{
    public function setUp() {
        parent::setUp();
        $this->hasMutator('password', '_encryptPassword');
    }

    protected function _encryptPassword($value) {
        $salt = $this->_get('salt');
        $this->_set('password', md5($salt . $value));
    }
}

and you using loadData() to fill your database from YAML file make sure you load salt field first as shown below:

ModelUser:
    User_Admin:
        username: admin
        salt: $secret_
        password: adminPa$$
        email: admin@promosquare.com

instead of:

ModelUser:
    User_Admin:
        username: admin
        password: adminPa$$
        salt: $secret_
        email: admin@promosquare.com
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜