How to install a custom mutator using the Doctrine YAML schema?

I use this tutorial as the basis for the Code Igniter / Doctrine project.

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

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

I got trapped because I have no idea how to present the following using YAML:

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

(this is from the user model code, in the "Adding Mutators" section) In particular, I am having problems with the $ this-> hasMutator line

I searched googled to blue, looked for documentation for Doctrine, symfony (which I know uses Doctrine heavily) and even grep'd for a database of mutator links - I came out empty

Is there any way to present the string $ this-> hasMutator ('password', '_encrypt_password'); using Doctrine YAML?

+3
source share
3 answers

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

You can get around this limitation by creating your own doctrine behavior. A doctrine of behavior can be assigned to your models in the YAML scheme. More details here:

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

In your case, the behavior will look something like this:

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

    public function _encrypt_password
    ....
}

And in the yaml schema:

EbonhandsModel:
  actAs: [EbonhandsTemplate]
+1
source

, User.php :

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

, (, YAML ), .

Olof /, , OO - " "

0

YAML .

, , User:

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));
    }
}

loadData(), YAML, , , :

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

:

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

Source: https://habr.com/ru/post/1777283/


All Articles