简体   繁体   English

Symfony原则批量插入

[英]Symfony doctrine bulk insert

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. 通过关系'AppBundle \\ Entity \\ Product#category'找到了一个新实体,该实体未配置为级联实体的持久操作: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"}). 解决此问题的方法:在此未知实体上显式调用EntityManager#persist()或配置级联,将该关联保留在映射中,例如@ManyToOne(..,cascade = {“ persist”})。 If you cannot find out which entity causes the problem implement 'AppBundle\\Entity\\Category#__toString()' to get a clue. 如果您找不到导致问题的实体,请实施“ AppBundle \\ Entity \\ Category #__ toString()”以获取线索。

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

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. 您也可以同时使用两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM