symfony - Symfony3 多对多关系和级联持久性。cascade={"persist"} 已配置但出现错误
问题描述
我在持久化对象时出错,说我需要在 ManyToMany 关系中配置级联持久化选项,但它已配置。
通过关系“AppBundle\Entity\ShopProducts#shopProductImages”找到了一个新实体,该实体未配置为对实体进行级联持久化操作:AppBundle\Entity\ShopProductImages@000000007d4db89e00000000344e8db2。要解决此问题:在此未知实体上显式调用 EntityManager#persist() 或配置级联在映射中保持此关联,例如 @ManyToOne(..,cascade={"persist"})。如果您无法找出导致问题的实体,请执行“AppBundle\Entity\ShopProductImages#__toString()”以获得线索。
$product = new ShopProducts();
$form = $this->createForm(ProductTypeNew::class, $product);
if ($form->isSubmitted() && $form->isValid())
{
$image = new ShopProductImages();
...
$product->addShopProductImages($image);
...
$em->persist($product);
$em->flush();
实体/ShopProducts.php
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(
* targetEntity="AppBundle\Entity\ShopProductImages",
* mappedBy="shopProducts",
* cascade={"persist"}
* )
*/
private $shopProductImages;
/**
* @return ArrayCollection|ShopProductImages[]
*/
public function getShopProductImages()
{
return $this->shopProductImages;
}
public function addShopProductImages(ShopProductImages $image)
{
if ($this->shopProductImages->contains($image)) {
return;
}
$this->shopProductImages[] = $image;
$image->addImagesProduct($this);
}
public function removeShopProductImages(ShopProductImages $image)
{
if (!$this->shopProductImages->contains($image)) {
return;
}
$this->shopProductImages->removeElement($image);
$image->removeImagesProduct($this);
}
实体/ShopProductImages.php
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\ShopProducts",
* inversedBy="shopProductImages",
* cascade={"persist"}
* )
* @ORM\JoinTable(name="shop_product_images_has_shop_products"),
* joinColumns={
* @ORM\JoinColumn(name="shop_product_images_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="shop_products_id", referencedColumnName="id")
* }
*/
private $shopProducts;
public function addImagesProduct(ShopProducts $product)
{
if ($this->shopProducts->contains($product)) {
return;
}
$this->shopProducts[] = $product;
$product->addShopProductImages($this);
}
public function removeImagesProduct(ShopProducts $product)
{
if (!$this->shopProducts->contains($product)) {
return;
}
$this->shopProducts->removeElement($product);
$product->removeShopProductImages($this);
}
表单/类型/ProductTypeNew.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('price')
->add('description', TextareaType::class)
->add('quantity')
->add('file', FileType::class, array('label' => 'Zdjęcie'))
解决方案
在 manyToMany 上,您不需要 cascade={"persist"}
你有你的 add..() 和 remove...() 它替换你的级联以将数据持久保存到中间表中
在您的表单构建器中,您需要: ->add('ShopProductImages', 'collection', array( 'by_reference' => false, ))
并从您的注释中删除级联
推荐阅读
- r - 如何增加标签的宽度 - BlueSky Statistics
- .net - 如何修复“连接未关闭。连接的当前状态:正在连接。”
- python - 最频繁连续数算法的起始索引
- vue.js - 如何使用具有存储调度功能的 vue-test-utils 和 jest 在组件中为 Vue 方法编写 Vue 测试用例
- office-ui-fabric - 为什么 Office UI Fabric 中有多个版本的日期时间控件?
- r - 将列组合成列表列
- apache-spark - pyspark - 从结构中获取第一个非空属性
- apache-kafka - 有没有办法知道kafka connect sink写了多少条记录?
- javascript - 使用 face-api.js 和 Photoshop 时出现问题
- ios - 多次触发时,我的软键盘上的高度会发生变化