我有一组标签,每个标签都有一组映射(一对多关系)。问题是当我尝试循环这个映射集合,然后我刷新我的实体管理器时,会创建所有标签的副本,我不明白为什么。
以下是批量大小的代码。第一个foreach循环标记集合,然后我得到这个标记的所有映射。然后,wenn我在foreach上刷新我得到一个例外,但是如果我在结束时(在for之后)只冲洗一次,它就可以了。你能帮我么?
以下是例外:
使用params [“Test 1”]执行'INSERT INTO标记(名称)VALUES(?,?,?)'时发生异常: SQLSTATE [23000]:完整性约束违规:1062重复条目“UNIQ_C09225D35E237E06”的重复条目“测试1”
这里是代码:
$batchSize = 100; //Entity Manager will flush every batchSize times
$i = 0;
foreach ($aTagCollection as $oTag) {
$aMapping = $oTag->getMapping();
foreach ($aMapping as $oMapping) {
}
if (($i % $batchSize) === 0) {
$this->em->flush(); //Execute all updates every 100 times
$this->em->clear(); //Detaches all objects from Doctrine
}
$i++;
}
$this->em->flush();
实体标签
/** @ORM\OneToMany(targetEntity="Mapping",
mappedBy="customerLabel", fetch="EXTRA_LAZY",
cascade={"persist"}, orphanRemoval=true)
*/
private $mapping;
public function __construct()
{
$this->mapping = new ArrayCollection();
}
public function addMapping(Mapping $mapping)
{
$this->mapping[] = $mapping;
return $this;
}
public function removeMapping(Mapping $mapping)
{
$this->mapping->removeElement($mapping);
}
public function getMapping()
{
return $this->mapping;
}
实体映射:
/**
* @ORM\ManyToOne(targetEntity="Tag", inversedBy="mapping", cascade={"persist"})
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $tag;
public function setTag(Tag $tag = null)
{
$this->tag= $tag;
return $this;
}
public function getTag()
{
return $this->tag;
}
非常感谢!
答案 0 :(得分:0)
customerLabel?
/** @ORM\OneToMany(targetEntity="Mapping",
mappedBy="tag", fetch="EXTRA_LAZY",
cascade={"persist"}, orphanRemoval=true)
*/