0

I have a form with a field for tags. Tags is a Doctrine ArrayCollection on the bound entity. The field is by_reference=false as the doc suggested, but binding the form to the entity behaves illegally while adding new element, like this:

$data=$entity->getTags(); //gets the ArrayCollection but does not care that it is not an array, and shoulrd be converted first
//do the value modifications like:
$data[]=new Tag(...);
$entity->setTags($data); //poor setter gets called with the already-updated collection, this operation is pointless

I figured that by_reference false was there to avoid this problem. If yes, then it's malfunctioning. If not, then the doc is very poor having an example for ArrayCollections but not caring about this very brutal neglection of setters...

What should I use instead? Returning a toArray() in getter is a no-go (obviouly it's not sane to design the model for compatibility with poor form implementation. Is there perhaps a type similar to 'collection' that forces conversion to array?

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Levente Pánczél
  • 1,895
  • 2
  • 14
  • 16
  • Sorry, I might have been confusing: the code in the question is meant to illustrate how FormType acts, it is not my code. I don't really have code to this particular aspect except $form->bindRequest(...), and a field definition. – Levente Pánczél Nov 01 '12 at 00:40
  • 2.0 (I would port to 2.1 but cannot afford it right now). – Levente Pánczél Nov 01 '12 at 10:35

1 Answers1

0

Add the tag to the entity as its supposed to happen:

$new_tag = new Tag(...);
$entity->addTag($new_tag);

There is no set-function for collections in a basic Doctrine generated entity.

Mats Rietdijk
  • 2,576
  • 3
  • 20
  • 25
  • 1) there is no addTag() and neither is it needed (so I won't create it, since the motto "the data model should be independent" is the air that I breathe - unless I wish to suffocate among symfonys terrible guidelines). 2) I have a setter. Doctrine generated it for me. And what I read in the docs docs says the same, so you might be mistaken. 3) You dig not understand a question. I don't want a solution instead of the code that I posted to illustrate what kind of failure FormType produces but a way to aviod the failure ... I obviously don't want to change FormType's code... – Levente Pánczél Nov 01 '12 at 00:44
  • Im not mistaken Doctrine generates a add-fuction instead of a set for MoreToSomething relations and yes i probably didnt understand your question as it seems in the code sample that you want to find ways to add extra "Tags". I dont think I have a anwser for what you try to find. Hope someone else can help you. – Mats Rietdijk Nov 01 '12 at 01:37
  • Thanks anyways. Sorry for my mistake, it is possible that the entities might have been generated with Symfony's generator rather than doctrine's. – Levente Pánczél Nov 01 '12 at 10:34