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