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)
var_dump) is$products->categoryvalid in your edit controller action? - cheesemacfly