我是Symfony2
的新手。我对One-To-Many
关系有疑问。
我的代码是,
此处,Product-Size,product_id返回null。
产品实体:
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;
}
}
尺寸实体:
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;
}
}
我的控制器:
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(),
));
}
我的观点:
...
<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>
产品形式和尺寸表:
产品表格:
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';
}
}
尺寸表格:
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';
}
}
问题: 我可以创造新的成功。但是size_ table的product_id返回Null。
帮助我。答案 0 :(得分:4)
当您在教条中保存实体时,拥有方必须保持关系:http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html
在您的代码中,拥有方是Size实体,因为参数上方的ManyToOne
描述始终是拥有方。这意味着您需要保留Size实体而不是产品实体。
我认为:
if ($form->isValid()){
//persist data to database
$em = $this->getDoctrine()->getEntityManager();
$product->upload();
// loop through as set the relationship for each size
foreach($product->getSize() as $size){
$size->setProduct($product);
}
$em->persist($product);
$em->flush();
//set Message Flash
$this->get('session')->setFlash('notice_success', 'Success Message ');
return $this->redirect($this->generateUrl('_admin_home'));
}
你的级联持续存在应该照顾其余的