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 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab