0
votes

I am learning symfony framework and have a big problem.

I have two database table: category (primary key: categoryid) and product (primary key: productid and foreign key: category_categoryid).

In the Controller add a "editAction" for edit the product data (include field category_categoryid) and save in DB.

The form must contain a categories dropdownlist for classify the product.

The code:

Relation in ORM yml

Entity: Product ...

manyToOne:
 category:
  targetEntity: Category
   inversedBy: products
   joinColumn:
    name: category_categoryid 
    referencedColumnName: categoryid

Entity: Category ...

oneToMany:
 products:
  targetEntity: Product
   mappedBy: category

Form/Type ProductType

public function buildForm(FormBuilderInterface $builder, array $options)
{
...

$builder->add('category_categoryid', 'entity',
array('class' => 'SysCatalogoBundle:Category',

'property' => 'name',));

...
}

ProductController

public function editAction(Request $request)
{
$id = 1;
$em = $this->getDoctrine()->getManager();
$products= $em->getRepository('SysCatalogoBundle:Product')->find($id);
$form = $this->createForm(new ProductType(), $products);


...
}

The view renderize a form with the list of categories in element, but not selected product category (saved in DB). I prove use type "query_builder" but didn't work, also tried with class "new Category" didn't work eather. If i use "choice" type with simpler array (array(1,2,3)) the category is selected.

Which could be the problem? The relation of ORM Entities? I follow the official symfony2 documentation.

I appreciate your help very very much! (sorry my english)

2
If you debug (or var_dump) is $products->category valid in your edit controller action? - cheesemacfly

2 Answers

1
votes

You are not using the orm mapping in your form builder. Try changing Form/Type ProductType to this:

public function buildForm(FormBuilderInterface $builder, array $options) { 
...

    $builder->add('category', 'entity', array());

... 
}
1
votes

I think the solution for this is pretty much covered in Symfony's official docu. http://symfony.com/doc/current/book/doctrine.html#add-mapping-information