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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵