0
votes

I am trying to optimize my doctrine save with bulk insert. But during the save i get the following error:

A new entity was found through the relationship 'AppBundle\Entity\Product#category' that was not configured to cascade persist operations for entity: AppBundle\Entity\Category@00000000351492f00000000072328419. 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\Category#__toString()' to get a clue.

My Category Entity:

class Category
{

    /**
     * @var string
     *
     * @ORM\Id()
     * @ORM\Column(type="string", nullable=false, unique=true)
     * @ORM\GeneratedValue(strategy="UUID")
     */
    private $id;

    /**
     * @var Category
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
     */
    private $parent;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Category", mappedBy="parent", fetch="EAGER")
     */
    private $children;

    /**
     * @var ArrayCollection;
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Product", mappedBy="category", fetch="EAGER")
     */
    private $products;

    /**
     * @var string
     * @ORM\Column(type="string", nullable=false)
     */
    private $title;
}

My Product Entity:

class Product
{

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false)
     */
    protected $category;

    /**
    * @ORM\Column(type="integer",nullable=true)
    */
    private $stock;

}

My save method:

$em = $this->getDoctrine()->getManager();
$batchCount = 10;
for ($i = 0; $i < 10000; $i++) {
     $product = new Product();
     $product->setName("Product A");
     $product->setCategory("category A");
     $em->persist($product);
     if (($i % $batchCount) == 0) {
         $em->flush();
         $em->clear();
    }
}
1

1 Answers

1
votes

First in your code:

$product->setCategory("category A");

You couldn't have this because it should require object, but i will assume you have simplified your code for sake of question.

Your problem is that you add association to product, but you don't add association to your category entity.

This has generally two solutions:

// In your Product entity add cascade persist, this will check your categories as well and schedule them for persist during flush
/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="products", cascade={"persist"})
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false)
 */
protected $category;

Next solution (I prefer this one, because you have all the time control around everything).

// In your product entity set Category
public function setCategory(Category $category) {
    $this->category = $category;
    $category->addProduct($this);
}

// In you Category entity
public function addProduct(Product $product) {
    if (!$this->products->contains($product)) {
        $this->product->add($product);
    }
}

You can use both at the same time as well.