dtds8802 2018-05-28 22:19
浏览 165
已采纳

Symfony主义批量插入

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条回答 默认 最新

  • duannaikuang1301 2018-05-29 01:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 广告联盟的兜底广告是什么意思
  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化