Tengo las siguientes entidades, Product y Category con relación de N - 1. en el formulario CategoryType añado los campos de Product para agregar uno mas.
pero cuando guardo en la tabla Product en el campo category_id queda en NULL.
ProductType:
Código PHP:
class CategoryType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('products', 'collection', array(
'type' => new ProductType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'prototype_name' => 'products',
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
// public function setDefaultOptions(OptionsResolver $resolver)
// {
// $resolver->setDefaults(array(
// 'data_class' => 'DeteccionBundle\Entity\Category'
// ));
// }
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DeteccionBundle\Entity\Category'
));
}
/**
* @return string
*/
public function getName()
{
return 'category';
}
}
Código PHP:
class ProductType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('price')
//->add('category')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DeteccionBundle\Entity\Product'
));
}
/**
* @return string
*/
public function getName()
{
return 'product';
}
}
Código HTML:
{% extends '::base.html.twig' %} {% block body -%} {{ form_start(form) }} {# {{ form(form) }} #} {{ form_widget(form) }} {{ form_end(form) }} <a href="#" id="add-another-email">Añadir otro producto</a> </ul> {% endblock %} {% block javascripts %} {{ parent() }} <script> // keep track of how many email fields have been rendered var emailCount = '{{ form.products|length }}'; jQuery(document).ready(function() { jQuery('#add-another-email').click(function(e) { e.preventDefault(); var productList = jQuery('#category_products'); // grab the prototype template var newWidget = productList.attr('data-prototype'); // replace the "__name__" used in the id and name of the prototype // with a number that's unique to your products // end name attribute looks like name="contact[products][2]" newWidget = newWidget.replace(/__name__/g, emailCount); emailCount++; // create a new list element and add it to the list var newLi = jQuery('<li></li>').html(newWidget); newLi.appendTo(productList); }); }) </script> {% endblock %}
Código PHP:
public function createAction(Request $request)
{
$entity = new Category();
$form = $this->createCreateForm($entity);
//$form->handleRequest($request);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('category_show', array('id' => $entity->getId())));
}
return $this->render('DeteccionBundle:Category:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}