Hello everyone,
I have a document, let's call it Post
which embeds many documents, all from the same collection, let's say Tag
. I need to dynamically add tags to a post when I am creating of through it's form.
I have already done it the relational way, following the recipe on the Symfony2 Cookbook.
However when I did so with Mongo I got this error:
Property "tags" is not public in class "Acme\TestBundle\Document\Post". Maybe you should create the method "setTags()"?
I looked in the Document code generated by doctrine:mongodb:generate:document
and as Symfony says there is no setTags
method. The code looks like:
/**
* @MongoDB\EmbedMany(targetDocument="Tag")
*/
protected $tags = array();
/**
* Add tags
*
* @param Acme\TestBundle\Document\Tag $tags
*/
public function addTags(\Acme\TestBundle\Document\Tag $tags)
{
$this->tags[] = $tags;
}
/**
* Get tags
*
* @return Doctrine\Common\Collections\Collection $tags
*/
public function getTags()
{
return $this->tags;
}
The exception was throw in the Controller, from the action used to implement the Create part of the CRUD, more precisely from within $form->bindRequest($request);
I worked this around by manually adding the setTags
in the Document, this way:
public function setTags(ArrayCollection $tags)
{
$this->tags = $tags;
}
The bind now works and validation and persist aswell. However I am not happy because I don't know if what I did is fine...
I am not very confident with Mongo and Symfony2 so maybe I am missing something, I mean if the generator didn't add the setTags method there must be a reason.
I am missing something here? Otherwise if I have found a problem I would be glad to help to fix it.