首页 > 解决方案 > Symfony3 多对多关系和级联持久性。cascade={"persist"} 已配置但出现错误

问题描述

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

通过关系“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);
}

表单/类型/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'))

标签: symfonydoctrine

解决方案


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

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

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

并从您的注释中删除级联


推荐阅读