0
votes

I got error while persisting object saying I need to configure cascade persist option in ManyToMany relation, but it is configured.

A new entity was found through the relationship 'AppBundle\Entity\ShopProducts#shopProductImages' that was not configured to cascade persist operations for entity: AppBundle\Entity\ShopProductImages@000000007d4db89e00000000344e8db2. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'AppBundle\Entity\ShopProductImages#__toString()' to get a clue.

enter image description here Controller/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();

Entity/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);
}

Entity/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'))
2
Have you tried adding $shopProductImages = New.... (...) $em->persist($shopProductImages); in your controllerGuillaume Harari
yes, this is in main post.Sruj
I was talking about $em->persist($shopProductImages), not $product. But if it works, I think it's a bad practice.Guillaume Harari
yeah I tried that, it persist data to both tables then but not to the middle table.Sruj

2 Answers

1
votes

In your controller, try adding $em->persist($image); before you flush.

Your issue seems to be that he can't create the values of the shop_product_images_has_shop_products table because the images don't have an Id yet, so you have to persist the $image and the $product entities before you flush.

Also, your cascade={"persist"} should only be in the image entity entity annotations, not on the product ones.

0
votes

On manyToMany, you don't need cascade={"persist"}

You have your add..() and remove...() It replace your cascade to persist data into the middle table

In your form builder you need : ->add('ShopProductImages', 'collection', array( 'by_reference' => false, ))

And remove cascades from your annotations