【发布时间】:2013-11-07 17:52:17
【问题描述】:
我有两个实体
评论和禁止评论
实体具有相同的字段,当一个评论被禁止时,从评论实体中删除对象并复制到BannedComments中
现在我使用这个脚本 Symfony 2 - Clone entity to different table
$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 是评论的实体
【问题讨论】:
-
您为什么不使用带有布尔值的
Comment实体将评论设置为禁止?