I’m new to Symfony2. I have problem about One-To-Many relationship.
My code is,
Here, Product-Size, product_id return null.
Product Entity :
namespace Front\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
class Product
{
//
/**
* @ORM\OneToMany(targetEntity="Size", mappedBy="product", cascade={"persist"})
*/
public $size;
public function __construct()
{
$this->size = new ArrayCollection();
}
/**
* Add size
*
* @param Front\ProductBundle\Entity\Size $size
*/
public function addSize(\Front\ProductBundle\Entity\Size $size)
{
$this->size[] = $size;
}
/**
* Get size
*
* @return Doctrine\Common\Collections\Collection
*/
public function getSize()
{
return $this->size;
}
}
Size Entity:
namespace Front\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Size
{
//
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="size", cascade={"persist"})
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
/**
* Set product
*
* @param Front\ProductBundle\Entity\Product $product
*/
public function setProduct(\Front\ProductBundle\Entity\Product $product)
{
$this->product = $product;
}
/**
* Get product
*
* @return Front\ProductBundle\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
}
My Controller:
namespace Admin\ProductBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
// Entity
use Front\ProductBundle\Entity\Product;
use Front\ProductBundle\Entity\Size;
use Admin\ProductBundle\Form\Type\ProductType;
use Admin\ProductBundle\Form\Type\SizeType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class AdminController extends Controller
{
public function createAction(Request $request)
{
$category = new Category();
$product = new Product();
//make 3 size for product
$size1 = new Size();
$product->addSize($size1);
$size2 = new Size();
$product->addSize($size2);
$size3 = new Size();
$product->addSize($size3);
$form = $this->createForm(new ProductType(), $product);
//write data
if($request->getMethod() == 'POST'){
$form->bindRequest($request);
//check validate
if ($form->isValid()){
//persist data() to database
$em = $this->getDoctrine()->getEntityManager();
$product->upload();
$em->persist($product);
$em->flush();
//set Message Flash and redirect
$this->get('session')->setFlash('notice_success', 'Success Message ');
return $this->redirect($this->generateUrl('_admin_home'));
}
}
return $this->render('AdminProductBundle:Admin:create.html.twig', array(
'form' => $form->createView(),
));
}
My View:
...
<div>
{% for size in form.size %}
<div id="sizeField">
<div class="blockSizeForm">
{{ form_label(size.name) }}
{{ form_widget(size.name) }}
</div>
<div class="blockSizeForm">
{{ form_label(size.price) }}
{{ form_widget(size.price) }}
</div>
</div>
{% endfor %}
</div>
Product Form and Size Form:
Product Form:
namespace Admin\ProductBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ProductType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('size', 'collection', array(
'type' => new SizeType(),
'allow_add' => true,
'by_reference' => false,
));
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Front\ProductBundle\Entity\Product',
'error_bubbling'=>true,
);
}
public function getName()
{
return 'Product';
}
}
Size Form:
namespace Admin\ProductBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class SizeType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name','text',array(
'label' => 'name:',
))
->add('price','money',array(
'label' => 'value:',
));
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Front\ProductBundle\Entity\Size',
'error_bubbling'=>true,
);
}
public function getName()
{
return 'Size';
}
}
PROBLEM:
I can create new success. But product_id for Size table return Null.
help me.
When you save entities in doctrine, the owning side must persist the relationship: http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html
In your code, the owning side is the Size entity as the entity with the
ManyToOnedescription above the parameter is always the owning side. This means you need to persist the Size entities not the product entities.I think something like:
Your cascade persist should take care of the rest