Php 如何更新条令中的数据?
我有一个视频类,与字幕类有一对多的关系 Video.orm.xmlPhp 如何更新条令中的数据?,php,doctrine,Php,Doctrine,我有一个视频类,与字幕类有一对多的关系 Video.orm.xml <doctrine-mapping> <entity name="VideoLibrary\Api\Infrastructure\Persistence\Doctrine\Entity\Video" table="video"> <id name="id" type="guid" />
<doctrine-mapping>
<entity name="VideoLibrary\Api\Infrastructure\Persistence\Doctrine\Entity\Video" table="video">
<id name="id" type="guid" />
<field name="title" type="string" length="300" />
<field name="duration" type="integer" length="300" />
<field name="status" type="string" length="20" />
<field name="createdAt" type="datetime" />
<field name="updatedAt" type="datetime" />
<one-to-many field="subtitles" target-entity="Subtitle" mapped-by="video">
<cascade>
<cascade-persist/>
</cascade>
<order-by>
<order-by-field name="language" direction="ASC"/>
</order-by>
</one-to-many>
</entity>
</doctrine-mapping>
视频数据的更新没有问题,问题是当将字幕集合传递给它时,它不执行更新,而是尝试仅在字幕中插入
$videoUpdate = $this->repository->findOneBy(['id'=>$video->getId()->getValue()]);
$videoUpdate->setTitle($video->getTitle());
$videoUpdate->setDuration($video->getDuration());
$videoUpdate->setStatus($video->getStatus()->getValue());
array_map(function($subtitle) use ($videoUpdate){
$videoUpdate->UpdateSubtitle($this->subtitleToInfrastructure($subtitle));
},$video->getSubtitles()->getCollection());
$this->entityManager->persist($videoUpdate);
$this->entityManager->flush();
UpdateSubtitle方法
public function UpdateSubtitle(Subtitle $subtitle):void{
$subtitle->setVideo($this);
foreach ($this->subtitles->toArray() as $item) {
$key = $this->subtitles->indexOf($item);
if($item->getId() === $subtitle->getId()){
$this->subtitles->set($key,$subtitle);
}else if(!$this->subtitles->contains($item)){
$this->subtitles->add($subtitle);
}
}
}
因为Persist for the video Entity会进行更新,但是当我将字幕实体的集合传递到视频实体中时,如果具有该id的字幕已经存在,它希望执行插入操作
$videoUpdate = $this->repository->findOneBy(['id'=>$video->getId()->getValue()]);
$videoUpdate->setTitle($video->getTitle());
$videoUpdate->setDuration($video->getDuration());
$videoUpdate->setStatus($video->getStatus()->getValue());
array_map(function($subtitle) use ($videoUpdate){
$videoUpdate->UpdateSubtitle($this->subtitleToInfrastructure($subtitle));
},$video->getSubtitles()->getCollection());
$this->entityManager->persist($videoUpdate);
$this->entityManager->flush();
public function UpdateSubtitle(Subtitle $subtitle):void{
$subtitle->setVideo($this);
foreach ($this->subtitles->toArray() as $item) {
$key = $this->subtitles->indexOf($item);
if($item->getId() === $subtitle->getId()){
$this->subtitles->set($key,$subtitle);
}else if(!$this->subtitles->contains($item)){
$this->subtitles->add($subtitle);
}
}
}