我陷入了一片混乱。
我正在尝试编写一种应接收user_id的方法,并检查该用户是否有配置文件更改请求。
如果存在(在用户表中将change_request字段设置为true),则应应用更改请求->应将具有该ID的更改请求表中的所有用户数据字段移至该用户表。
我的服务
public function getUserApplyChangeRequest($id)
{
$a =$this->getUserRepository()->find($id);
$b =$this->getChangeProfileRequestRepository()->find($id);
$b = clone $a;
$this->em->persist($b);
$this->em->flush();
}
我的控制器。
public function userApplyChangeRequestAction($changeRequest)
{
$this->requirePostParams(['user_id']);
if ($changeRequest === 1){
$applyChange = $this->get('user')->getUserApplyChangeRequest($this->getUser());
}
return $this->success();
}
我需要帮助,因为我被困住了,并不真正知道如何使用这些代码行,但是我举了一个我想发生的事的例子。
答案 0 :(得分:0)
如果只有五个,最简单的方法是自行设置属性:
public function getUserApplyChangeRequest($id)
{
$a =$this->getUserRepository()->find($id);
$b =$this->getChangeProfileRequestRepository()->find($id);
$a->setPropertyOne($b->getPropertyOne());
$a->setPropertyTwp($b->getPropertyTwo());
$this->em->persist($a);
$this->em->flush();
}
其他选择是使用原则获取变更对象的所有属性,并以这种方式调用getter / setter方法(未经测试,请确保添加NULL检查并跳过ID字段):
$props = $em->getClassMetadata(get_class($b))->getColumnNames();
foreach($props as $prop){
//get value from B
$reflectionMethod = new ReflectionMethod(get_class($b),'get'.ucfirst($prop));
$value = $reflectionMethod->invoke($b);
//set value in A
$reflectionMethod = new ReflectionMethod(get_class($a),'set'.ucfirst($prop));
$reflectionMethod->invoke($a, $value);
}