我有两个实体
评论和禁止评论
实体具有相同的字段,当一个评论被禁止时,从评论实体中删除对象并复制到 BannedComments
现在我使用这个脚本 Symfony 2 - 将实体克隆到不同的表
$oldEntity = $oldEntity;
$newEntity = new NewEntity();
$oldReflection = new \ReflectionObject($oldEntity);
$newReflection = new \ReflectionObject($newEntity);
foreach ($oldReflection->getProperties() as $property) {
if ($newReflection->hasProperty($property->getName())) {
$newProperty = $newReflection->getProperty($property->getName());
$newProperty->setAccessible(true);
$newProperty->setValue($newEntity, $property->getValue($oldEntity));
}
}
但我必须将所有变量更改为公共...
有更好的方法来复制内容吗?
我尝试使用克隆
$BannedComments = new BannedComments();
$BannedComments = clone $Comments;
$em->persist($BannedComments);
但是保存在评论中而不是在 BannedComments 中,因为当我克隆评论时,BannedComments 是评论的实体