Bonsoir,
J'ai encore une fois un problème de formulaire, de liste déroulante et de clé étrangère. Je m'explique :
J'ai un formulaire d'inscription, via le plugin sfForkedDoctrineApplyPlugin, que j'ai étendu via une table "Profil". Cette table contient un ensemble de champs, et 3 clés étrangères, prenons la clé sports_id se référent à la table Sports, par exemple.
Voici mon schéma.yml:
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
| Profils:
inheritance:
type: column_aggregation
extends: sfGuardUserProfile
actAs: [ Timestampable, SoftDelete ]
columns:
id:
type: integer
fixed: false
unsigned: false
primary: true
autoincrement: true
birthday:
type: date(25)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
codepostal:
type: integer
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
adresse:
type: string(255)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
ville:
type: string(255)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
departements_id:
type: integer
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
nationalite:
type: string(100)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
sports_id:
type: integer
fixed: false
unsigned: false
primary: false
notnull: true
fonctions_id:
type: integer
fixed: false
unsigned: false
primary: false
notnull: true
autoincrement: false
relations:
Departements:
local: departements_id
foreign: id
type: one
Sports:
local: sports_id
foreign: id
type: one
....
Sports:
actAs: [ Timestampable, SoftDelete ]
tableName: sports
columns:
id:
type: integer
fixed: false
unsigned: false
primary: true
autoincrement: true
name:
type: string(100)
fixed: false
unsigned: false
primary: false
notnull: true
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:
Profils:
local: id
foreign: sports_id
type: many |
Lorsque j'affiche le formulaire, cette fois-ci, le champ sports_id n'est pas une liste déroulante mais un champs de saisie de texte. J'ai effectué quelques tests, en m'inscrivant, en entrant dans ce champ l'id d'un des sports (1 par exemple), cela fonctionne parfaitement.
Cependant, c'est pas idéal de demander à l'utilisateur de taper un chiffre comme ça, j'ai donc personnaliser le widget pour mettre une liste déroulante:
1 2 3 4 5
| $this->widgetSchema['sports_id'] = new sfWidgetFormChoice(array(
'choices' => Sports::getSportsId(),
'multiple' => false,
'expanded' => false,
)); |
La fonction getSportsId() associée:
1 2 3 4 5 6 7 8
| public static function getSportsId()
{
$q = Doctrine_Query::create()
->select('s.id')
->from('Sports s');
return $q->execute();
} |
Le nom du sport étant stocké dans la colonne "name", ma liste déroulante affiche bien le nom du sport, cependant, lorsque je soumet le formulaire j'ai l'erreur suivante:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`monagentgdev`.`sf_guard_user_profile`, CONSTRAINT `sf_guard_user_profile_sports_id_sports_id` FOREIGN KEY (`sports_id`) REFERENCES `sports` (`id`))
Une idée de comment résoudre cela, je m'arrache un peu les cheveux, je ne vois pas par où je pourrais résoudre ce problème ...
Comment faire pour que Symfony repère cette clé étrangère et mette automatiquement une liste déroulante ?
Merci
Partager