Php Symfony/Doctrine仅在更新对象之间保留echo
我试图在symfony中设置一个路由,它只是根据给定的请求参数“enter”将数据库中的值从0更新为1,反之亦然 这是我迄今为止编写的代码:Php Symfony/Doctrine仅在更新对象之间保留echo,php,symfony,doctrine,Php,Symfony,Doctrine,我试图在symfony中设置一个路由,它只是根据给定的请求参数“enter”将数据库中的值从0更新为1,反之亦然 这是我迄今为止编写的代码: public function enter(Request $request) { $response = new JsonResponse(); $data = [ "success" => true ]; try { $params = $this->ht
public function enter(Request $request) {
$response = new JsonResponse();
$data = [
"success" => true
];
try {
$params = $this->httpHelper->checkParams([
"enter" => true
], $request);
$location = $this->entityManager
->getRepository(Location::class)
->find($this->getUser()->getLocation()->getId());
// echo $location->getName();
if ($params["enter"]) {
$location->setEnter(true);
}
$this->entityManager->persist($location);
$this->entityManager->flush();
} catch (\Exception $e) {
$response->setStatusCode(500);
$data = [
"message" => $e->getMessage(),
"trace" => $this->getParameter("kernel.environment") == "dev" ? $e->getTrace() : []
];
}
return $response->setData($data);
}
如您所见,从数据库中读取属于当前用户的location对象。如果请求参数(“enter”)为true,则值“enter”将更改为true,并且对象将再次保留到数据库中。
因此,假设“enter”的当前数据库值为0(false),并且我正在向给定路由发送一个带有参数“enter=true”的请求-理论上,数据库中的值现在应该为1。但事实并非如此——什么都没有发生
但是,如果在从数据库读取对象后添加“echo$location->getName()”(请参见代码中注释掉的行),它会突然工作,这对我来说毫无意义,我只是不明白为什么会发生这种情况
有人有主意吗?很抱歉,如果有些描述让人困惑,这是我关于stackoverflow的第一个问题:D如果有任何东西让人困惑,请告诉我