Bonjour,
Je suis en train de développer un site en PHP Objet selon le modèle MVC.
J'ai décomposé la partie Modèle en DTO/DAO/Services selon un modèle que j'ai vu en cours de Java (DTO=classes-entités représentant les tables ; DAO=classes permettant de manipuler les entités ; Services=classes contenant les actions métier).
J'utilise l'ORM Doctrine 2.2.0, PHP version 5.3.0, MYSQL version 5.1.36.
Principes :
Un "membre" et un "admin" existent en base de données qui héritent tous deux de "utilisateur" qui contient le login, le password et le status (mysql ne permettant pas l'héritage, en fait un clé étrangère ramène vers le bon enregistrement dans "utilisateur").
Cela fait donc 3 DTO, AbstractUserDto représente la table utilisateur héritée par membre et admin.
Dans le fichier principal doctrine-2.2.0_test.php, je crée l'objet membre dont je valorise les champs "login", "password", "id-connexion" (status), "prénom" et "nom" pour faire les tests.
J'ai bien vérifié que $oMemberDto et $entityManager sont des objets (cf /*..*/ in UserDao.class.php).
Dans le try{...} de UserDao.class.php, je commence la transaction (des echo sont placés après chaque étape afin de vérifier ce qui semble fonctionner et ce qui bloque) mais au moment de faire un persist() du DTO, il y a une exception qui est soulevée (affichage alors de l'echo du catch) alors que lorsque persist() est commenté, le try semble s'exécuter jusqu'au bout (j'ai bien tous les echo attendus qui s'affichent sur la page).
Pour autant, les données de l'objet ne sont jamais copiées en base de données. Je ne sais donc pas ce qui ne marche pas puisque d'ailleurs dans les docs sur doctrine, il est bien question de faire un persist() puis un flush().
Pourriez-vous m'aider à y voir plus clair ?
Je vous reproduis une partie du code ci-dessous (seulement les fichiers nécessaires pour cette partie du programme mais ils sont reproduits ici intégralement).
Par avance merci.
doctrine-2.2.0_test.php (fichier principal d'exécution du script)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php // inclusion of Doctrine parameters require_once '/modeles/dao/Bootstrap.class.php'; /* * MAIN * here we test the access to the data */ try { // creation of the user's DAO $oUserDao = new DaoManager(); $oUserDao = $oUserDao->getUserDao(); // hydratation of the user's DTO $oMemberDto = new DtoManager(); $oMemberDto = $oMemberDto->getUserDtoFactory()->createMember(); $oMemberDto->setLogin("jeandin75"); $oMemberDto->setPassword("jeanPass"); $oMemberDto->setStatus(0); $oMemberDto->setFirstName("Jean"); $oMemberDto->setLastName("Dujardin"); echo '<p>name : '.$oMemberDto->getFirstName().' '.$oMemberDto->getLastName().'</p>'; // creation of the user into the database $oUserDao->create($oMemberDto); echo '<p>name : '.$oMemberDto->getFirstName().' '.$oMemberDto->getLastName().'</p>'; } catch (Exception $e) { echo $e->getTrace(); } echo '<--- fin de programme ---><br />'; ?>
Bootstrap.class.php :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php require_once '/lib/doctrine-2.2.0/Doctrine/ORM/Tools/Setup.php'; /* ** Loading the entities (DTO) */ require_once '/modeles/dto/DtoManager.class.php'; require_once '/modeles/dto/user/AbstractUserDto.class.php'; require_once '/modeles/dto/user/AdminUserDto.class.php'; require_once '/modeles/dto/user/MemberUserDto.class.php'; require_once '/modeles/dto/user/UserDtoFactory.class.php'; /* ** Loading the DAOs */ require_once '/modeles/dao/DaoManager.class.php'; require_once '/modeles/dao/user/UserDao.class.php'; use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; final class Bootstrap { private $lib, $paths, $isDevMode, $config, $connectionOptions; private $databaseName, $dbUser, $dbUserPassword, $dbHost, $pdoDriver; public function __construct () { $this->lib = "/lib/doctrine-2.2.0"; Doctrine\ORM\Tools\Setup::registerAutoloadDirectory($this->lib); $this->paths = array($this->lib); $this->isDevMode = true; $this->databaseName = 'randos-poitou'; $this->dbUser = 'visitor'; $this->dbUserPassword = 'He7ppnX4'; $this->dbAdmin = 'admin'; $this->dbAdminPassword = 'fJ5AbGS5'; $this->dbHost = 'localhost'; $this->pdoDriver = 'pdo_mysql'; // database configuration parameters $this->connectionOptions = array( 'dbname' => $this->databaseName, 'user' => $this->dbAdmin, 'password' => $this->dbAdminPassword, 'host' => $this->dbHost, 'driver' => $this->pdoDriver, ); // Create a simple "default" Doctrine ORM configuration for Annotation Mapping $this->config = Setup::createAnnotationMetadataConfiguration($this->paths, $this->isDevMode); } // Creation of an Entity Manager public function createEntityManager () { return EntityManager::create($this->connectionOptions, $this->config); } } ?>
AbstractUserDto.class.php (contenant la classe héritée de MemberUserDto)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php /* ** Abstract class of an User of the Website ** with its data compulsory to any kind of user */ /** ** @Entity ** @Table(name="utilisateur") ** @Inheritance(strategy=InheritanceType.JOINED) **/ abstract class AbstractUserDto { /** @Id @Column(type="integer") @GeneratedValue **/ protected $id; /** @Column(name="login" type="string" length=20) **/ protected $login; /** @Column(name="password" type="string" length=20) **/ protected $password; /** @Column(name="id-connection" type="integer") **/ protected $status; protected $line; // getting a data public function getId () { return $this->id; } public function getLogin () { return $this->login; } public function getPassword () { return $this->password; } public function getStatus () { return $this->status; } // updating a data public function setId ($id) { $this->id = $id; } public function setLogin ($login) { $this->login = $login; } public function setPassword ($password) { $this->password = $password; } public function setStatus ($status) { $this->status = $status; } // getting the connection details into a HTML table public function __toString () { $line = "<table>"; $line .= "<tr><th>label</th><th>value</th></tr>"; $line .= "<tr><td>id</td><td>".$this->id."</td></tr>"; $line .= "<tr><td>login</td><td>".$this->login."</td></tr>"; $line .= "<tr><td>password</td><td>".$this->password."</td></tr>"; $line .= "<tr><td>status</td><td>".$this->status."</td></tr>"; $line .= "</table>"; return $line; } } ?>
MemberUserDto.class.php (contient l'entité réplique de la table 'membre') :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php /* ** class of a Member of the Website ** and its specific data */ /** * @Entity * @Table(name="membre") **/ class MemberUserDto extends AbstractUserDto { // variables necessary to the Member /** @Id @Column(type="integer") @GeneratedValue **/ protected $id; /** @Id @Column(name="id-utilisateur" type="integer") **/ protected $userId; /** @Column(name="prenom" type="string" length=50) **/ protected $firstName; /** @Column(name="nom" type="string" length=50) **/ protected $lastName; /** @Column(name="orientation" type="integer" nullable="true") **/ protected $orientation; /** @Column(name="adresse" type="string" nullable="true" length=100) **/ protected $address; /** @Column(name="zipCode" type="integer" nullable="true") **/ protected $zipCode; /** @Column(name="ville" type="string" nullable="true" length=50) **/ protected $town; /** @Column(name="nom" type="string" nullable="true" length=10) **/ protected $telephone; /** @Column(name="adresse-email" type="string" length=255) **/ protected $email; /** @Column(name="visibilite" type="string" length=3) **/ protected $visibility; /** @Column(name="id-bureau" type="integer" nullable="true") **/ protected $memberOfAdministration; /** @Column(name="date-debut-fonction" type="date" nullable="true") **/ protected $dateOfStartInAdmin; protected $line; // constructor public function __construct () { } // getting a data public function getFirstName () { return $this->firstName; } public function getLastName () { return $this->lastName; } public function getOrientation () { return $this->orientation; } public function getAddress () { return $this->address; } public function getZipCode () { return $this->zipCode; } public function getTown () { return $this->town; } public function getTelephone () { return $this->telephone; } public function getEmail () { return $this->email; } public function getVisibility () { return $this->visibility; } public function getMemberOfAdministration () { return $this->memberOfAdministration; } public function getDateOfStartInAdmin () { return $this->dateOfStartInAdmin; } // updating a data public function setFirstName ($firstName) { $this->firstName = $firstName; } public function setLastName ($lastName) { $this->lastName = $lastName; } public function setOrientation ($orientation) { $this->orientation = $orientation; } public function setAddress ($address) { $this->address = $address; } public function setZipCode ($zipCode) { $this->zipCode = $zipCode; } public function setTown ($town) { $this->town = $town; } public function setTelephone ($telephone) { $this->telephone = $telephone; } public function setEmail ($email) { $this->email = $email; } public function setVisibility ($visibility) { $this->visibility = $visibility; } public function setMemberOfAdministration ($memberOfAdministration) { $this->memberOfAdministration = $memberOfAdministration; } public function setDateOfStartInAdmin ($dateOfStartInAdmin) { $this->dateOfStartInAdmin = $dateOfStartInAdmin; } // getting the connection details into a HTML table public function __toString () { $line = "<table>"; $line .= "<tr><th>label</th><th>value</th></tr>"; $line .= "<tr><td>id</td><td>".$this->id."</td></tr>"; $line .= "<tr><td>userID</td><td>".$this->userId."</td></tr>"; $line .= "<tr><td>first name</td><td>".$this->firstName."</td></tr>"; $line .= "<tr><td>last name</td><td>".$this->lastName."</td></tr>"; $line .= "<tr><td>login Id</td><td>".$this->login."</td></tr>"; $line .= "<tr><td>password Id</td><td>".$this->password."</td></tr>"; $line .= "<tr><td>status</td><td>".$this->status."</td></tr>"; $line .= "<tr><td>orientation</td><td>".$this->orientation."</td></tr>"; $line .= "<tr><td>address</td><td>".$this->address."</td></tr>"; $line .= "<tr><td>zipCode</td><td>".$this->zipCode."</td></tr>"; $line .= "<tr><td>town</td><td>".$this->town."</td></tr>"; $line .= "<tr><td>telephone</td><td>".$this->telephone."</td></tr>"; $line .= "<tr><td>email</td><td>".$this->email."</td></tr>"; $line .= "<tr><td>visibility</td><td>".$this->visibility."</td></tr>"; $line .= "<tr><td>memberOfAdministration</td><td>".$this->memberOfAdministration."</td></tr>"; $line .= "<tr><td>dateOfStartInAdmin</td><td>".$this->dateOfStartInAdmin."</td></tr>"; $line .= "</table>"; return $line; } } ?>
UserDtoFactory.class.php (set à créer l' UserDto)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php /* ** creation of an User */ class UserDtoFactory { public function __construct () { } // creation of an Administrator public function createAdmin () { $adminDto = new AdminUserDto(); $adminDto->setStatus(0); return $adminDto; } // creation of a Member public function createMember () { $memberDto = new MemberUserDto(); $memberDto->setStatus(0); $this->memberOfAdministration = 0; return $memberDto; } } ?>
DtoManager.class.php (gestion des DtoFactories) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php require_once 'user/AbstractUserDto.class.php'; require_once 'user/UserDtoFactory.class.php'; require_once 'user/AdminUserDto.class.php'; require_once 'user/MemberUserDto.class.php'; /* ** Manager of the DTO Factories */ class DtoManager { protected $_user_dto_factory; protected $_administrative_function_dto_factory; protected $_connection_status_dto_factory; protected $_orientation_dto_factory; protected $_document_dto_factory; protected $_document_type_dto_factory; protected $_take_part_dto_factory; protected $_outgoing_dto_factory; protected $_outgoing_type_dto_factory; // construction of factory Objects public function __construct () { $this->_user_dto_factory = new UserDtoFactory(); } // getting a factory Object public function getUserDtoFactory() { return $this->_user_dto_factory; } } ?>
UserDao.class.php (accès aux données de l' UserDto et lien avec la bdd) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php class UserDao { protected $entityManager; protected $client; // constructor public function __construct () {} /* ** creation of an user into the Database from its Dto */ public function create ($userDto) { $this->entityManager = new Bootstrap(); $this->entityManager = $this->entityManager->createEntityManager(); echo '$userDto\'s last name : '.$userDto->getLastName().'<br />'; $userDto->setLastName('Jardin'); echo 'becomes : '.$userDto->getLastName().'<br />'; try { $this->entityManager->getConnection()->beginTransaction(); echo 'after beginTransaction()<br />'; /* $this->entityManager->persist($userDto); echo 'after persist($userDto)<br />'; */ $this->entityManager->flush(); echo 'after flush()<br />'; } catch (Exception $e) { $this->entityManager->rollback(); echo 'after rollback()<br />'; } $this->entityManager->close(); } /* ** reading an user from its ID */ public function read ($userId) { $this->entityManager = new Bootstrap(); $this->entityManager = $this->entityManager->createEntityManager(); // creation of an empty user $client = null; try { $this->entityManager->beginTransaction(); $this->client = $this->entityManager->find ($userId); $this->entityManager->flush(); } catch (Exception $e) { $this->entityManager->rollback(); } $this->entityManager->close(); return $this->client; } } ?>
DaoManager.class.php (gestion des DAO) :
type=
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 <?php class DaoManager { protected $_user_dao; public function __construct () { $this->_user_dao = new UserDao(); } // getting an object public function getUserDao () { return $this->_user_dao; } } ?>
Partager