EDIT for google searchers: It's impossible to persist in one controller and to flush in another one.
I have two controllers, called by ajax. The first one is called when a change occurs in the twig, this is where I persist.
The second one is called when the "save" button is clicked, and that's when I call flush.
Unfortunately, it does not change anything in the database.
Here are my controllers:
public function update_accountAction(Request $request)
{
Try
{
$code = $request->request->get('code');
$name = $request->request->get('name');
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('NRtworksChartOfAccountsBundle:Accounttree');
$to_change = new Accounttree();
$to_change = $repo->findOneByCode($code);
$to_change->setName($name);
$to_change->setCode($code);
$em->persist($to_change);
$response = array("code" => 100, "success" => true, "modified" => $code);
return new Response(json_encode($response));
}
Catch(Exception $e)
{
$response = array("code" => 100, "success" => false, "error" => $e);
return new Response($response);
}
}
The next one:
public function save_changesAction()
{
Try
{
$em = $this->getDoctrine()->getManager();
$em->flush();
$response = array("code" => 100, "success" => true);
return new Response(json_encode($response));
}
Catch(Exception $e)
{
$response = array("code" => 100, "success" => false, "error" => $e);
return new Response($response);
}
$response = array("code" => 100, "success" => true);
return new Response(json_encode($response));
}
Apparently no errors are occurring but no change is done..
I was thinking the problem is that the entity manager is limited to one controller but I did not succeed in call it outside...
Help ?
ps: I have a test controller in which I do persist and flush and the change is done in the DB.
persist()
calls aren't remembered between requests.
Therefore you can't call persist()
in one request and flush()
in a subsequent request.
You will need to implement a caching logic (i.e. saving the entities marked for persistence in the session) to make this work.