gpt4 book ai didi

forms - OneToMany relation_id 返回 Null

转载 作者:行者123 更新时间:2023-12-01 12:48:07 25 4
gpt4 key购买 nike

我是 Symfony2 的新手。我对一对多关系有疑问。

我的代码是,

这里,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;
}
}

我的 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(),
));
}

我的观点:

...
<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 表的 product_id 返回 Null。

帮帮我。

最佳答案

当您在原则中保存实体时,拥有方必须保持关系:http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html

在您的代码中,拥有方是 Size 实体,因为在参数上方具有 ManyToOne 描述的实体始终是拥有方。这意味着您需要保留尺寸实体而不是产品实体。

我觉得是这样的:

      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'));
}

你的级联持久化应该处理剩下的

关于forms - OneToMany relation_id 返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14168769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com