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

Embedform and many to many Symfony

  • SOLVED

Hello

[[LINK href="http://img41.imageshack.us/img41/9355/bdd.png"]]http://img41.imageshack.us/img41/9355/bdd.png[[/LINK]]

I made an embedForm for categories when i add a Post.

//in PostForm.class.php
$this->embedForm('category', new CategoryForm());




When adding a new post, if I do not choose any category, i create new category with my embedForm.

When I validate the form, my post is inserted into db, and the category, therefore nothing is impacted in my table PostCategory to associate my category with my post..

Thanks for your help.

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


Category:
actAs:
Sluggable:
unique: true
fields: [name]
canUpdate: true
notnull: true
columns:
id:
type: integer(8)
primary: true
notnull: true
autoincrement: true
name:
type: string(32)
notnull: true
description:
type: clob
parent:
type: integer(8)
notnull: true
default: 0
count:
type: integer(4)
notnull: true
default: 0

PostCategory:
columns:
post_id:
type: integer(8)
primary: true
notnull: true
cat_id:
type: integer(8)
primary: true
notnull: true
relations:
Post:
class: Post
local: post_id
foreign: id
foreignAlias: PostCategories
onDelete: CASCADE
Category:
class: Category
local: cat_id
foreign: id
foreignAlias: PostCategories
onDelete: CASCADE

Answers (2)

2010-04-27

Karol Sójko answers:

You have to overwrite your bind form of the postForm with something like this:


public function bind(array $taintedValues = null, array $taintedFiles = null)
{
$output = parent::bind($taintedValues, $taintedFiles);
$posts = embeddedForms['category']->getObject()->getPosts();
$posts->add($this->getObject());
$this->embeddedForms['category']->getObject()->setPosts($posts);

foreach ($this->embeddedForms as $name => $form) {
$this->embeddedForms[$name]->isBound = true;
$this->embeddedForms[$name]->values = $this->values[$name];
}

return $output;
}



jeromebazin comments:

Both answers are good, the winner will be the one will fix this little problem first.

If I create a post with an empty required field , I get a notice (so only in dev).

Notice: Undefined index: category for this line.
$ This-> embeddedForms [$ name] -> values = $ this-> values [$ name];


jeromebazin comments:

After thinking this answer is more complete.

2010-04-27

Alex Zgorzhelsky answers:

I think something like this might help in your Post form
public function bind(array $taintedValues = null, array $taintedFiles = null){

if(empty($taintedValues['category']['name'])){

unset($this->embeddedForms['category'], $taintedValues['category']);
$this->validatorSchema['category'] = new sfValidatorPass();
}else{
// here is your code.......................

}


$output = parent::bind($taintedValues, $taintedFiles);
foreach ($this->embeddedForms as $name => $form) {
$this->embeddedForms[$name]->isBound = true;
$this->embeddedForms[$name]->values = $this->values[$name];
}
return $output;
}


jeromebazin comments:

Both answers are good, the winner will be the one will fix this little problem first.

If I create a post with an empty required field , I get a notice (so only in dev).

Notice: Undefined index: category for this line.
$ This-> embeddedForms [$ name] -> values = $ this-> values [$ name];