[Doctrine2 icm CodeIgniter 2] Wrappers maken

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • Matis
  • Registratie: Januari 2007
  • Laatst online: 11-09 20:27

Matis

Rubber Rocket

Topicstarter
Beste PRGers,

Momenteel ben ik bezig een webtool te schrijven, welke gebaseerd is op Doctrine2 (2.1.1) en CodeIgniter 2 (2.1.2) welke ik gecloned heb van https://github.com/wildly...Igniter-2-with-Doctrine-2
Nu heb ik een volgende entity gemaakt:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace Entity;
/* application/models/Entity/User.php */
/**
 * User Model
 *
 * @Entity
 * @Table(name="users")
 */
class User
{
    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Column(type="string", length=32, unique=true, nullable=false)
     */
    protected $name;

    /**
     * @Column(type="string", length=128, nullable=false)
     */
    protected $hashed_password;

    /**
     * @Column(type="string", length=255, unique=true, nullable=false)
     */
    protected $email;

Middels
./application/doctrine orm:generate-entities models
wordt de entity op de volgende manier gevormd
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/* application/models/Entity/User.php */
namespace Entity;

/**
 * User Model
 *
 * @Entity
 * @Table(name="users")
 */
class User
{
    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Column(type="string", length=32, unique=true, nullable=false)
     */
    protected $name;

    /**
     * @Column(type="string", length=128, nullable=false)
     */
    protected $hashed_password;

    /**
     * @Column(type="string", length=255, unique=true, nullable=false)
     */
    protected $email;
  
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set hashed_password
     *
     * @param string $hashedPassword
     * @return User
     */
    public function setHashedPassword($hashedPassword)
    {
        $this->hashed_password = $hashedPassword;
        return $this;
    }

    /**
     * Get hashed_password
     *
     * @return string 
     */
    public function getHashedPassword()
    {
        return $this->hashed_password;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }
}

Nu wil ik graag een wrapper model / class / functie schrijven welke een plaintext password omzet naar een hashed variant en zodoende kan valideren.
Ik dacht het volgende te kunnen doen.
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* application/models/Entity/UserWrapper.php */

namespace Entity;

class UserWrapper extends User
{
    public function __construct()
    {
        parent::__construct();
    }

    public function setPassword($plaintext)
    {
        $hashed_password = $this->hashPassword($plaintext);
        $this->setHashedPassword($hashed_password);
    }

    private function hashPassword($password)
    {
        $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
        $hash = hash('sha256', $salt . $password);

        return $salt . $hash;
    }
}

Maar als ik vanuit de controller de setPassword functie wil aanroepen op het UserWrapper object krijg ik de volgende foutmelding
( ! ) Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class Entity\UserWrapper is not a valid entity or mapped super class.' in /home/matis/wwwroot/ci_checklists/application/libraries/Doctrine/ORM/Mapping/MappingException.php on line 147
( ! ) Doctrine\ORM\Mapping\MappingException: Class Entity\UserWrapper is not a valid entity or mapped super class. in /home/matis/wwwroot/ci_checklists/application/libraries/Doctrine/ORM/Mapping/MappingException.php on line 147


Hoe kan ik dit realiseren? Wat ik liever niet wil is de functie binnen de User class aanmaken, omdat deze telkens geupdated wordt gedurende de ontwikkeling.

Matis

If money talks then I'm a mime
If time is money then I'm out of time


Acties:
  • 0 Henk 'm!

  • Seikeau
  • Registratie: December 2011
  • Laatst online: 01-09-2024
Ben niet zo thuis in PHP, maar moet je de User class juist niet de UserWrapper class laten extenden?

Acties:
  • 0 Henk 'm!

Verwijderd

Ik heb geen kaas gegeten van Doctrine, maar misschien is het hier beter om een compositie te gebruiken ipv overerving.

Wikipedia: Composition over inheritance

Acties:
  • 0 Henk 'm!

  • Bee.nl
  • Registratie: November 2002
  • Niet online

Bee.nl

zoemt

De hint is al gegeven in de foutmelding ;) Heb je al naar Inheritance Mapping gekeken in de Doctrine manual?

Bovenstaande code werkt namelijk alleen als je de UserWrapper aanmerkt als @entity en de User class als @MappedSuperclass. Let wel op dat de super class zelf geen entity is, maar alleen mapping informatie bevat.
Wat ik liever niet wil is de functie binnen de User class aanmaken, omdat deze telkens geupdated wordt gedurende de ontwikkeling.
Je kunt gewoon (custom) methods in je entity class opgegeven zonder dat Doctrine daar aan zit bij het genereren van je entities. Ik zie daarom ook niet in waarom je hier een wrapper voor zou willen gebruiken. Het is juist de bedoeling dat je dergelijke methods in de User class onderbrengt.
Generate entity classes and method stubs from your mapping information.

$ php doctrine orm:generate-entities
$ php doctrine orm:generate-entities --update-entities
$ php doctrine orm:generate-entities --regenerate-entities

This command is not suited for constant usage. It is a little helper and does not support all the mapping edge cases very well. You still have to put work in your entities after using this command.

It is possible to use the EntityGenerator on code that you have already written. It will not be lost. The EntityGenerator will only append new code to your file and will not delete the old code. However this approach may still be prone to error and we suggest you use code repositories such as GIT or SVN to make backups of your code.

[ Voor 12% gewijzigd door Bee.nl op 25-09-2012 14:46 ]


  • Cartman!
  • Registratie: April 2000
  • Niet online
Gewoon omdraaien, je entity die wrapper laten extenden en dan werkt het prima, daar heb je geen extra mappings voor nodig. Overigens ben ik t met mijn voorganger eens dat je die methods gewoon op je entity zelf kunt zetten (tenzij het librarywerk wordt).

Zelf pas ik de entities aan en gebruik ik de Codegen-functie van NetBeans om de getters/setters te genereren, dan wordt er niks overgeschreven verder.