Bien le bonjour,
Je possède dans un projet symfony2 une relation bidirectionnel entre deux Class avec un attribut
Un type de produit peut avoir plusieurs options et celle-ci dans un ordre donnée.
Dans ma class Type j'ai rajouter la relation comme suit :
1 2 3 4
| /**
* @ORM\OneToMany(targetEntity="Ana\EcommerceBundle\Entity\TypeOptionArticle", mappedBy="type",cascade={"persist", "remove"})
*/
private $joinOptions; |
J'ai aussi créer la class intermédiaire
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class TypeOptionArticle {
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Ana\EcommerceBundle\Entity\TypeArticle", inversedBy="joinOptions")
*/
protected $type;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Ana\EcommerceBundle\Entity\OptionArticle", inversedBy="joinTypes")
*/
protected $option;
/**
* @ORM\Column(name="ordre", type="integer")
*/
private $ordre; |
Et j'ai ensuite déclaré mon FormTYpe comme suit :
1 2 3 4 5 6 7 8 9 10 11 12
| public function buildForm(FormBuilder $builder, array $options) {
$builder
->add('name', 'text', array(
'label' => 'catalog_type.field.name'
))
->add('joinOptions', 'collection', array(
'type' => new TypeOptionArticleType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false));
;
} |
Je n'ai aucun soucis pour afficher le formulaire ou pour l'éditer.
Mais j'ai une erreur lors de l'ajout d'un élément dans la collection.
spl_object_hash() expects parameter 1 to be object, array given
L'erreur se déroule lors du cascade persist
1 2
| UnitOfWork ->doPersist (array('ordre' => '1', 'option' => object(OptionArticle)), array('000000002843d36900000000e705080a' => object(TypeArticle)))
in /Doctrine/ORM/UnitOfWork.php at line 1788 |
Des idées ???
Partager