【问题标题】:Symfony3 ManyToMany relation and cascade persistance. cascade={"persist"} is configured but error occursSymfony3 多对多关系和级联持久性。 cascade={"persist"} 已配置但出现错误
【发布时间】:2018-04-25 13:47:25
【问题描述】:

我在持久化对象时出错,说我需要在多对多关系中配置级联持久化选项,但它已配置。

通过关系“AppBundle\Entity\ShopProducts#shopProductImages”找到了一个新实体,该实体未配置为对实体进行级联持久化操作:AppBundle\Entity\ShopProductImages@000000007d4db89e00000000344e8db2。要解决此问题:在此未知实体上显式调用 EntityManager#persist() 或配置级联在映射中保持此关联,例如 @ManyToOne(..,cascade={"persist"})。如果您无法找出导致问题的实体,请执行 'AppBundle\Entity\ShopProductImages#__toString()' 以获得线索。

控制器/ShopController.php

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

Form/Type/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'))

【问题讨论】:

  • 你试过添加 $shopProductImages = New.... (...) $em->persist($shopProductImages);在你的控制器中
  • 是的,这是在主帖中。
  • 我说的是 $em->persist($shopProductImages),而不是 $product。但如果它有效,我认为这是一个不好的做法。
  • 是的,我试过了,它将数据保存到两个表,但不保存到中间表。
  • 我认为你的分析器在这里:afilina.com/doctrine-not-saving-manytomany

标签: symfony doctrine


【解决方案1】:

在您的控制器中,尝试在刷新之前添加$em->persist($image);。

您的问题似乎是他无法创建 shop_product_images_has_shop_products 表的值,因为图像还没有 Id,因此您必须在您之前坚持 $image 和 $product 实体冲洗。

另外,您的cascade={"persist"} 应该只在图片实体实体注释中,而不是在产品注释中。

【讨论】:

  • 它解决了我的问题。它将 id 保存到中间表 shop_product_images_has_shop_products。我很确定我之前也尝试过。
【解决方案2】:

在多对多上,您不需要 cascade={"persist"}

你有你的 add..() 和 remove...() 它替换了你的级联以将数据持久保存到中间表中

在您的表单构建器中,您需要: ->add('ShopProductImages', 'collection', array( 'by_reference' => false, ))

并从注释中删除级联

【讨论】:

  • “清理注释”是什么意思?删除cascade={"persist"}?它没有帮助
  • 这就是我的意思。您已在两个实体上删除它?
  • 在您的表单构建器中,您有这个:->add('ShopProductImages', 'collection', array( 'by_reference' => false, ))
  • 在我的表单中,我故意不使用 ShopProductImages 实体,因为我需要使用 FileType 字段,因为我想在表单中上传文件。
  • 当 2018 年的答案解决了我在 2022 年的问题时
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
  • 2011-11-28
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多