Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php [Symfony][doctor]批处理-具有多通关系的批量插入=错误:通过该关系找到了新实体_Php_Symfony_Doctrine - Fatal编程技术网

Php [Symfony][doctor]批处理-具有多通关系的批量插入=错误:通过该关系找到了新实体

Php [Symfony][doctor]批处理-具有多通关系的批量插入=错误:通过该关系找到了新实体,php,symfony,doctrine,Php,Symfony,Doctrine,我有两个具有多个关系的实体:播放列表和播放视频。一个播放列表可以有多个视频。播放视频属于播放列表。数据库中已存在播放列表 我尝试使用批量插入,但我得到: A new entity was found through the relationship 'App\Entity\Playlist\PlaylistVideo#playlist' that was not configured to cascade persist operations for entity: App\Entity\Pla

我有两个具有多个关系的实体:播放列表和播放视频。一个播放列表可以有多个视频。播放视频属于播放列表。数据库中已存在播放列表

我尝试使用批量插入,但我得到:

A new entity was found through the relationship 'App\Entity\Playlist\PlaylistVideo#playlist' that was not configured to cascade persist operations for entity: App\Entity\Playlist\Playlist__1. 
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"}).
播放列表实体:

class Playlist
{
    /**
     * @ORM\OneToMany(targetEntity=PlaylistVideo::class, mappedBy="playlist")
     * @Groups({"playlist"})
     */
    private Collection $videos;

    ...

    public function addVideo(PlaylistVideo $video): self
    {
        if (!$this->videos->contains($video)) {
            $this->videos[] = $video;
            $video->setPlaylist($this);
        }

        return $this;
    }
播放视频实体:

class PlaylistVideo
{
    /**
     * @ORM\ManyToOne(targetEntity=Playlist::class, inversedBy="videos")
     * @ORM\JoinColumn(nullable=false)
     */
    private Playlist $playlist;

    ...

    public function setPlaylist(Playlist $playlist): self
    {
        $this->playlist = $playlist;

        return $this;
    }
批处理-批量插入:

    /**
     * Batch Processing - Bulk Inserts
     * @param VideoDto[] $videos
     */
    public function bulkInserts(Playlist $playlist, array $videos)
    {
        $this->em->getConnection()->getConfiguration()->setSQLLogger(null);

        $videosAmount = count($videos);
        $batchSize = 50;
        for ($i = 0; $i < $videosAmount; $i++) {
            $video = $videos[$i];

            $playlistVideo = (new PlaylistVideo())
                ->setPlaylist($playlist)
                ->setVideoId($video->id)
                ->setTitle($video->title);

            $this->em->persist($playlistVideo);

            if (($i % $batchSize) === 0) {
                $this->em->flush();
                $this->em->clear();
            }
        }

        $this->em->flush();
        $this->em->clear();
    }
/**
*批处理-批量插入
*@param VideoDto[]$videos
*/
公共功能插件(播放列表$播放列表,阵列$视频)
{
$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
$videosAmount=计数($videos);
$batchSize=50;
对于($i=0;$i<$videosAmount;$i++){
$video=$videos[$i];
$PlayVideo=(新的PlayVideo())
->setPlaylist($playlist)
->setVideoId($video->id)
->setTitle($video->title);
$this->em->persist($playlidvideo);
如果($i%$batchSize)==0){
$this->em->flush();
$this->em->clear();
}
}
$this->em->flush();
$this->em->clear();
}
我无法添加
cascade={“persist”}
(在播放视频
@ORM\ManyToOne()
)中),因为我不希望最后有很多播放列表

我什么都试过了,但我不能只把视频添加到播放列表中。

你可以调用$this->em->clear();并在其后面使用相同的播放列表实体。EntityManager已经不知道了。你需要重新加载它

for ($i = 0; $i < $videosAmount; $i++) {
    $video = $videos[$i];

    $playlistVideo = (new PlaylistVideo())
        ->setPlaylist($playlist)
        ->setVideoId($video->id)
        ->setTitle($video->title);

    $this->em->persist($playlistVideo);

    if (($i % $batchSize) === 0) {
        $this->em->flush();
        $this->em->clear();

        // Reload the Playlist entity after clearing the EntityManager
        $playlist = $this->em->getRepository(Playlist::class)->find($playlist->getId());
    }
}
for($i=0;$i<$videosAmount;$i++){
$video=$videos[$i];
$PlayVideo=(新的PlayVideo())
->setPlaylist($playlist)
->setVideoId($video->id)
->setTitle($video->title);
$this->em->persist($playlidvideo);
如果($i%$batchSize)==0){
$this->em->flush();
$this->em->clear();
//清除EntityManager后重新加载播放列表实体
$playlist=$this->em->getRepository(playlist::class)->find($playlist->getId());
}
}

谢谢!它起作用了!我只将这一新行更改为
$playlist=$this->em->getReference(playlist::class,$playlist->getId())