many to many sur la même table avec doctrine

29 views
Skip to first unread message

smellycat37

unread,
Oct 30, 2009, 6:33:44 AM10/30/09
to Symfony-fr
Hello,

Je ne trouve pas de solution pour créer une many to many sur la même
table.
Je créer mon schema.yml, je génère mes objects, mes forms... Aucun
pb... Mes relations sont bien crées en base...
J'édite un élément, j'ai bien ma multi selecbox.... aucun pb...
c'est au moment de la sauvegarde d'un product ou l'on a ajouté des
frères ou j'ai une erreur 500 :
Not null violation: 7 ERROR: null value in column "product_brother_id"
violates not-null constraint

schema.yml
----------------------------------------------------------------------------------------------------------------------------------
Product:
actAs:
Timestampable: ~
columns:
id: {type: string(4), primary: true, notnull: true}
title: {type: string(200), notnull: true}
relations:
Products:
class: Product
refClass: ProductBrother

ProductBrother:
columns:
product_id: {type: string(20), primary: true, notnull: true}
product_brother_id: {type: string(20), primary: true, notnull:
true}
relations:
Product: {onDelete: CASCADE, class: Product, local: product_id,
foreign: id}
ProductBrother: {onDelete: CASCADE, class: Product, local:
product_brother_id, foreign: id}
----------------------------------------------------------------------------------------------------------------------------------

J'ai regardé d'où pouvait venir l'erreur afin de trouver une solution
et voici mes conclusions:
1 - lors du $productForm->save();
2 - la method saveProductsList déclarée dans mon
BaseProductForm.clss.php est appelée
A la fin de cette method est appelée l'instruction
$this->object->link('Products', array_values($link));
3 - la method link est déclarée dans l'object Record (vendor\symfony
\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Record.php)
et c'est là d'où provient l'erreur :

foreach ($ids as $id) {
$record = new $modelClassName;
$record[$localFieldName] = $identifier;
$record[$foreignFieldName] = $id; //
$record->save();
}

$localFieldName est bien égale à product_id.
le pb est que $foreignFieldName est également égale à product_id alors
qu'il devrait être égale à product_brother_id.
J'imagine que cette erreur est du à mon schema.yml.

Merci à ceux qui pourront m'aidé


smellycat37

unread,
Oct 30, 2009, 7:33:13 AM10/30/09
to Symfony-fr
Hello,

juste pour poster une solution... je ne sais pas si c'est la
meilleur...
J'ai modifier mon schema.yml
schema.yml
----------------------------------------------------------------------------------------------------------------------------------
Product:
actAs:
Timestampable: ~
columns:
id: {type: string(4), primary: true, notnull: true}
title: {type: string(200), notnull: true}
relations:
Products:
class: Product
refClass: ProductBrother
foreign: product_brother_id
local: product_id

ProductBrother:
columns:
product_id: {type: string(20), primary: true, notnull: true}
product_brother_id: {type: string(20), primary: true, notnull:
true}
----------------------------------------------------------------------------------------------------------------------------------

Le seul hic c'est qu'à la genération de ProductForm.class.php, il y a
un widget créer pour products_list (product au pluriel) et un pour
product_list. cela est surement un comportement normal...

Lélio ML

unread,
Oct 30, 2009, 7:37:19 AM10/30/09
to symfo...@googlegroups.com
Oui c'est normal (même si je n'ai pas encore fait de many to many sur un
même table avec Doctrine.
Mais sur les tables liées, symfony génère pour chaque lien le widget qui
permet d'afficher les objets liés

Lélio
www.lelio.fr

-----Message d'origine-----
De : symfo...@googlegroups.com [mailto:symfo...@googlegroups.com] De la
part de smellycat37
Envoyé : vendredi 30 octobre 2009 12:33
À : Symfony-fr
Objet : [symfony-fr] Re: many to many sur la même table avec doctrine
Reply all
Reply to author
Forward
0 new messages