IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

Formulaire d'une table avec 2 clefs primaires [1.x]


Sujet :

Symfony PHP

  1. #1
    Membre à l'essai
    Femme Profil pro
    Développeur Web
    Inscrit en
    Juillet 2012
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2012
    Messages : 12
    Points : 13
    Points
    13
    Par défaut Formulaire d'une table avec 2 clefs primaires
    Bonjour,

    J'ai cherché partout, mais je n'ai pas trouvé la solution à mon problème.

    J'ai deux tables (tr_composant et tr_noeud) liées par une relation many-to-many, j'ai donc crée une table "entre les deux" (tr_composant_noeud).

    Voici le schéma :

    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
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
     
    TrNoeud:
      connection: doctrine
      tableName: tr_noeud
      columns:
        nd_id:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: true
          autoincrement: true
        nd_code:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        nd_nom:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        nd_description:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: false
          autoincrement: false
        nd_lft:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: false
          notnull: false
          autoincrement: false
        nd_rgt:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: false
          notnull: false
          autoincrement: false
        nd_nlevel:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: false
          notnull: false
          autoincrement: false
        nd_code_createur:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        nd_code_modifieur:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: false
          autoincrement: false
        created_at:
          type: timestamp(25)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        updated_at:
          type: timestamp(25)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        nd_ndt_id:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
      relations:
        TrComposantNoeud:
          local: nd_id
          foreign: com_nd_nd_id
          type: many
     
    TrComposant:
      connection: doctrine
      tableName: tr_composant
      columns:
        com_id:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: true
          autoincrement: true
        com_code:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        com_nom:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        com_description:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: false
          autoincrement: false
        com_code_createur:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        com_code_modifieur:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: false
          autoincrement: false
        created_at:
          type: timestamp(25)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        updated_at:
          type: timestamp(25)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
      relations:
        TrComposantNoeud:
          local: com_id
          foreign: com_nd_com_id
          type: many
     
    TrComposantNoeud:
      connection: doctrine
      tableName: tr_composant_noeud
      columns:
        com_nd_com_id:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: true
          autoincrement: false
        com_nd_nd_id:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: true
          autoincrement: false
        com_nd_code_createur:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        com_nd_code_modifieur:
          type: string(255)
          fixed: false
          unsigned: false
          primary: false
          notnull: false
          autoincrement: false
        created_at:
          type: timestamp(25)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        updated_at:
          type: timestamp(25)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
      relations:
        TrNoeud:
          local: com_nd_nd_id
          foreign: nd_id
          type: one
        TrComposant:
          local: com_nd_com_id
          foreign: com_id
          type: one
    Et voici mon formulaire :

    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
     
    class TrComposantNoeudForm extends BaseTrComposantNoeudForm
    {
      public function setup()
      {
          parent::setup();
          unset(
              $this['com_nd_com_id'],
              $this['com_nd_nd_id'],
          );
     
          $this->widgetSchema['com_nd_nd_id']= new sfWidgetFormDoctrineChoice(array('model' => 'TrNoeud', 'add_empty' => false) );
          $this->widgetSchema['com_nd_com_id']= new sfWidgetFormDoctrineChoice(array('model' => 'TrComposant', 'add_empty' => false) );
     
          $this->setValidators(array(
              'com_nd_nd_id'    => new sfValidatorDoctrineChoice(array('model' =>  $this->getModelName(), 'column' => 'com_nd_nd_id')),
              'com_nd_com_id'   => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'com_nd_com_id')),
           ));     
     
          $this->widgetSchema->setLabels(array(
          'com_nd_com_id'       => 'Nom du composant',
          'com_nd_nd_id'        => 'Noeud',
          ));
     
     
      }
     
    }
    Et la base du formulaire :

    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
     
    abstract class BaseTrComposantNoeudForm extends BaseFormDoctrine
    {
      public function setup()
      {
        $this->setWidgets(array(
          'com_nd_com_id'         => new sfWidgetFormInputHidden(),
          'com_nd_nd_id'          => new sfWidgetFormInputHidden(),
          'com_nd_code_createur'  => new sfWidgetFormInputText(),
          'com_nd_code_modifieur' => new sfWidgetFormInputText(),
          'created_at'            => new sfWidgetFormDateTime(),
          'updated_at'            => new sfWidgetFormDateTime(),
        ));
     
        $this->setValidators(array(
          'com_nd_com_id'         => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'com_nd_com_id', 'required' => false)),
          'com_nd_nd_id'          => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'com_nd_nd_id', 'required' => false)),
          'com_nd_code_createur'  => new sfValidatorString(array('max_length' => 255)),
          'com_nd_code_modifieur' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
          'created_at'            => new sfValidatorDateTime(),
          'updated_at'            => new sfValidatorDateTime(),
        ));
     
        $this->widgetSchema->setNameFormat('tr_composant_noeud[%s]');
     
        $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
     
        $this->setupInheritance();
     
        parent::setup();
      }
     
      public function getModelName()
      {
        return 'TrComposantNoeud';
      }
     
    }

    Mon problème est : lorsque je crée une nouvelle entité de tr_composant_noeud, un message d'erreur apparaît :
    SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'com_nd_nd_id' cannot be null

    C'est comme si le formulaire n'arrivait pas à trouver le noeud que j'ai sélectionné dans la liste.

    Si vous avez des idées, je prend !

    En vous remerciant de prendre le temps de regarder mon problème,
    Bonne journée!

  2. #2
    Membre éprouvé Avatar de Herode
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2005
    Messages
    825
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2005
    Messages : 825
    Points : 933
    Points
    933
    Par défaut
    Le schéma est faux. Une relation ne doit être déclarée que d'un seul côté, pas sur les deux tables. En général, on la déclare dans la table qui héberge la clé étrangère (mais c'est une convention plutôt qu'une obligation).

    En outre, les relations n-n ne se déclarent pas comme ça : cf. la doc Doctrine http://docs.doctrine-project.org/pro...#relationships

    PS : si possible, quand tu donnes un schema sur les forums, vire toutes les sections inutiles (valeurs par défaut). Avoir un schema de 200 lignes quand 30 suffisent, ça fait fuir

  3. #3
    Membre à l'essai
    Femme Profil pro
    Développeur Web
    Inscrit en
    Juillet 2012
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2012
    Messages : 12
    Points : 13
    Points
    13
    Par défaut
    Ok merci, je vais essayer ça.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. sqlite select sur table avec plusieurs clefs primaire
    Par Nono1nd dans le forum Android
    Réponses: 4
    Dernier message: 08/06/2012, 11h34
  2. Réponses: 9
    Dernier message: 08/10/2010, 08h39
  3. MAJ d'un champ d'une table avec condition sur clef primaire commune
    Par ar|equin dans le forum Requêtes et SQL.
    Réponses: 3
    Dernier message: 15/05/2007, 13h57
  4. creation d'une table avec une clé primaire multiple
    Par oursquetaire dans le forum Oracle
    Réponses: 2
    Dernier message: 23/12/2005, 13h29
  5. [postgresql]creer une table avec plusieurs clés primaire??
    Par perlgirl dans le forum Langage SQL
    Réponses: 2
    Dernier message: 09/11/2004, 17h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo