Ask your Symfony questions! Pay money and get answers fast! (more info)

Multiple embedforms and many to many Symfony

  • SOLVED

Hello.

This is my database : http://img41.imageshack.us/img41/9355/bdd.png

I made a tag embedform in my post form.


$tag_form = new TagForm();
$this->embedForm('tag1', $tag_form);


This is my doUpdateObject for my PostForm class.


public function doUpdateObject($values)
{
parent::doUpdateObject($values);
$posts = $this->embeddedForms['tag1']->getObject()->getPosts();xdebug_break();
$posts->add($this->getObject());
$this->embeddedForms['tag1']->getObject()->setPosts($posts);

}


When i submit the form all is good and my tag is linked with my post.

Then i am trying with a second embed.


$tag_form = new TagForm();
$this->embedForm('tag1', $tag_form);
$this->embedForm('tag2', $tag_form);


Now i have a problem, when i am trying the below code, only the second tag is inserted and linked in my database


public function doUpdateObject($values)
{
parent::doUpdateObject($values);
$posts = $this->embeddedForms['tag1']->getObject()->getPosts();xdebug_break();
$posts->add($this->getObject());
$this->embeddedForms['tag1']->getObject()->setPosts($posts);

$posts = $this->embeddedForms['tag2']->getObject()->getPosts();xdebug_break();
$posts->add($this->getObject());
$this->embeddedForms['tag2']->getObject()->setPosts($posts);

}

OR

foreach ($this->embeddedForms as $name => $form) {

$posts = $this->embeddedForms[$name]->getObject()->getPosts();
$posts->add($this->getObject());
$this->embeddedForms[$name]->getObject()->setPosts($posts);

}


How do i make to add and link dynamically tags in my database?

Cheers


My YML


Post:
actAs:
Sluggable:
unique: true
fields: [title]
canUpdate: true
notnull: true
columns:
id:
type: integer(8)
primary: true
notnull: true
autoincrement: true

.....

relations:
PostCategories:
class: Category
refClass: PostCategory
local: post_id
foreign: cat_id
foreignAlias: Posts





Tag:
actAs:
Sluggable:
unique: true
fields: [name]
canUpdate: true
notnull: true
columns:
id:
type: integer(4)
primary: true
notnull: true
autoincrement: true
name:
type: string(32)
notnull: true
unique: true
relations:
Posts:
class: Post
foreignAlias: Tags
refClass: PostTag
local: tag_id
foreign: post_id
options:
charset: utf8
collate: utf8_unicode_ci

PostTag:
columns:
post_id:
type: integer(4)
primary: true
notnull: true
autoincrement: false
tag_id:
type: integer(4)
primary: true
notnull: true
relations:
Post:
class: Post
local: post_id
foreign: id
foreignAlias: PostTags
onDelete: CASCADE
Tag:
class: Tag
local: tag_id
foreign: id
foreignAlias: PostTags
onDelete: CASCADE
options:
charset: utf8
collate: utf8_unicode_ci

Answers (2)

2010-05-20

Baruch Peña answers:

you are using the same object... why don't you use two objects??

$tag_form = new TagForm();
$tag_form2 = new TagForm();

$this->embedForm('tag1', $tag_form);

$this->embedForm('tag2', $tag_form2);


jeromebazin comments:

OMG, shame on me ....

Thanks

2010-05-20

Martin Palacio answers:

This link may help you, it's the unique "official" reference about using multiple embedded forms:
http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms