Bonjour,
Bon, voyant que mon précédent message est resté sans réponse "problème avec persist() et flush()", je poste un nouveau sujet avec un cas plus simple à regarder.
Soit une base de données Mysql regroupant 3 tables :
- connexion (primary key "id", "statut");
- membre (primary key "id" auto_increment, "prenom" varchar=255 null, "nom" varchar=255 null);
- utilisateur (primary key "id", "login" varchar=20 null auto_increment, "password" varchar=8 null, foreign key "id_connexion" int, foreign key "id_membre" int);
et des scripts PHP :
- Bootstrap.class.php : charge les DTOs, les contrôleurs, définit les éléments Doctrine à utiliser et définit également les identifiants de la bdd et la fonction de création de l'EntityManager;
- ConnectionDto.class.php : regroupe les propriétés de la table "connexion";
- AbstractUserDto.class.php : regroupe les propriétés de la table "utilisateur";
- MemberDto.class.php : regroupe les propriétés de la table "membre" et hérite de la classe AbstractUserDto;
- DefaultController.class.php : contient la fonction servant à créer un nouveau membre, à y valoriser certaines variables et à faire l'enregistrement en bdd;
- index.php : la page principale qui crée le DefaultController et appelle sa fonction de création du nouveau membre;
Il s'agit d'un exercice simple mais pourtant je bute sur l'erreur :
Je ne comprends pas où peut être le problème puisque le même exercice réalisé avec une appli "animal" selon les mêmes modalités fonctionne parfaitement. Un rapprochement entre les deux ne m'a pas permis d'identifier l'explication de cette erreur.Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Duplicate definition of column 'id' on entity 'utilisateurs\modeles\dto\MemberDto' in a field or discriminator column mapping.' in F:\MY WORKSTATION\DEV\WEB\utilisateurs\lib\doctrine-2.2.0\Doctrine\ORM\Mapping\MappingException.php:246 Stack trace: #0 F:\MY WORKSTATION\DEV\WEB\utilisateurs\lib\doctrine-2.2.0\Doctrine\ORM\Mapping\ClassMetadataInfo.php(1044): Doctrine\ORM\Mapping\MappingException::duplicateColumnName('utilisateurs\mo...', 'id') #1 F:\MY WORKSTATION\DEV\WEB\utilisateurs\lib\doctrine-2.2.0\Doctrine\ORM\Mapping\ClassMetadataInfo.php(1732): Doctrine\ORM\Mapping\ClassMetadataInfo->_validateAndCompleteFieldMapping(Array) #2 F:\MY WORKSTATION\DEV\WEB\utilisateurs\lib\doctrine-2.2.0\Doctrine\ORM\Mapping\Driver\AnnotationDriver.php(323): Doctrine\ORM\Mapping\ClassMetadataInfo->mapField(Array) #3 F:\MY WORKSTATION\DEV\WEB\utilisateurs\lib\doctrine-2.2.0\Doctrine\ORM\Mapping\ClassMetadataFactory.php(293): Doctrine\ORM\Mapping\Driver\A in F:\MY WORKSTATION\DEV\WEB\utilisateurs\lib\doctrine-2.2.0\Doctrine\ORM\Mapping\MappingException.php on line 246
Merci d'avance pour votre aide.
___________________________________________________________________________________________________________________________________________
Voici les scripts ci-dessous :
Bootstrap.class.php
ConnectionDto.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
61
62
63
64 <?php require_once '/lib/doctrine-2.2.0/Doctrine/ORM/Tools/Setup.php'; /* ** Loading the entities (DTO) */ require_once '/modeles/dto/AbstractUserDto.class.php'; require_once '/modeles/dto/MemberDto.class.php'; require_once '/modeles/dto/ConnectionDto.class.php'; /* ** Loading the controllers */ require_once '/vues/DefaultController.class.php'; use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping as ORM; 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 = 'utilisateurs'; $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); } //public function destroy () { } } ?>
AbstractUserDto.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 <?php namespace utilisateurs\modeles\dto; use Doctrine\ORM\Mapping as ORM, Doctrine\Common\Collections\ArrayCollection, Doctrine\Common\Persistence\PersistentObject; /** * @Entity * * @Table(name="connexion") */ class ConnectionDto { /** * @Id * @Column(name="id", type="integer") * @GeneratedValue(strategy="AUTO") * * @var int the identifier */ protected $id; /** * @Column(name="`statut`", type="integer") * * @var string the label of the status */ protected $status; /** @return int the identifier **/ public function getId () { return $this->id; } /** @return int the label of the status **/ public function getStatus () { return $this->status; } public function setId ($id) { $this->id = $id; } public function setStatus ($status) { $this->status = $status; } } ?>
MemberDto.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
61
62
63
64
65
66
67
68
69
70
71
72 <?php namespace utilisateurs\modeles\dto; use Doctrine\ORM\Mapping as ORM, Doctrine\Common\Collections\ArrayCollection, Doctrine\Common\Persistence\PersistentObject; use utilisateurs\modeles\dto\ConnectionDto; /** * @Entity * @InheritanceType("JOINED") * @DiscriminatorColumn(name="id_membre", type="integer") * @DiscriminatorMap({"utilisateur" = "AbstractUserDto", "membre" = "MemberDto"}) * * @Table(name="utilisateur") */ abstract class AbstractUserDto { /** * @Id * @Column(name="id", type="integer") * @GeneratedValue(strategy="AUTO") * * @var int the identifier of the entity */ protected $abstractUserId; /** * @Column(name="login", type="string", length=255, nullable=true) * * @var string the user login */ protected $login; /** * @Column(name="password", type="string", length=255, nullable=true) * * @var string the user password */ protected $password; /** * @var int the connection status of the user which is the id of the connection table */ protected $status; /** /** * @OneToOne(targetEntity="MemberDto", inversedBy="id") * @JoinColumn(name="id_membre", referencedColumnName="id") * * @var int the user identifier related to this member */ protected $memberId; /** @return int this user identifier **/ public function getUserId () { return $this->abstractUserId; } /** @return string this user login **/ public function getLogin () { return $this->login; } /** @return string this user password **/ public function getPassword () { return $this->password; } /** @return string this user status **/ public function getStatus () { return $this->status; } /** @return int the identifier of the member record this user is related to **/ public function getMemberId () { return $this->memberId; } public function setUserId ($abstractUserId) { $this->abstractUserId = $abstractUserId; } public function setLogin($login) { $this->login = $login; } public function setPassword($password) { $this->password = $password; } public function setStatus($status) { $this->status = $status; } public function setMemberId ($memberId) { $this->memberId = $memberId; } } ?>
DefaultController.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 <?php namespace utilisateurs\modeles\dto; use Doctrine\ORM\Mapping as ORM, Doctrine\Common\Collections\ArrayCollection, Doctrine\Common\Persistence\PersistentObject; use utilisateurs\modeles\dto\AbstractUserDto; /** * @Entity * * @Table(name="membre") */ class MemberDto extends AbstractUserDto { /** * @Id * @Column(name="id", type="integer") * @GeneratedValue(strategy="AUTO") * @OneToOne(targetEntity="AbstractUserDto", mappedBy="memberId") * * @var int the identifier */ protected $id; /** * @Column(name="prenom", type="string", length=255, nullable=true) * * @var string this member's first name */ protected $firstName; /** * @Column(name="nom", type="string", length=255, nullable=true) * * @var string this member's last name */ protected $lastName; public function __construct ($login, $password) { $this->setLogin ($login); $this->setPassword ($password); $this->setStatus (0); } /** @return int this member identifier **/ public function getId () { return $this->id; } /** @return string this member's first name **/ public function getFirstName () { return $this->firstName; } /** @return int this member's last name **/ public function getLastName () { return $this->lastName; } public function setId ($id) { $this->id = $id; } public function setFirstName ($firstName) { $this->firstName = $firstName; } public function setLastName ($lastName) { $this->lastName = $lastName; } } ?>
index.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 <?php use Symfony\Bundle\FrameworkBundle\Controller\Controller; use utilisateurs\modeles\dto\AbstractUserDto; use utilisateurs\modeles\dto\MemberDto; use utilisateurs\modeles\dto\ConnectionDto; /** * @Entity */ class DefaultController { /** * @return string the data of this new record */ public function createAction () { /** @var object this member data **/ $member = new MemberDto ("user1", "pass1"); $member->setFirstName ('User-ONE'); $member->setLastName ('PAss-1'); $member->setMemberId($member->getId()); $em = new Bootstrap(); $em = $em->createEntityManager(); $em->persist ($member); $em->flush (); return 'ok'; /* return 'Created a member under user reference : '.$member->getUserId().'<br />'. 'and member reference : '.$member->getMemberId().'<br />'. 'of name '.$member->getFirstName().' '.$member->getLastName().'<br />'. 'and these credentials :<br />'. 'login : '.$member->getLogin().'<br />'. 'password : '.$member->getPassword().'<br />' ; */ } } ?>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <?php require_once '/modeles/dao/Bootstrap.class.php'; $action = new DefaultController (); echo $action->createAction (); ?>
Partager