Issue
I am using symfony 3.4.8 version. When I am updating form data I want to see the differences before submit and after submit.
After $formEdit->handleRequest($request); line old value is changing to the new value but I don't want it.
but I could not do this.
Here is my controller.
/**
* @Route("/contract/{contractId}/Edit", name="contract_edit")
*/
public function editContract(Request $request, $contractId)
{
$log[]='';
$contractInfo = $this->getDoctrine()->getRepository('AppBundle:myContract\Contract')->find($contractId);
$formEdit = $this->createForm(ContractEditType::class, $contractInfo);
$log[0]= $contractInfo;
dump($log[0]); // Old value is coming here
$formEdit->handleRequest($request);
if ($formEdit->isSubmitted() && $formEdit->isValid()) {
$log[1]= $contractInfo; //new value is comming
dump($log[0]);
dump($log[1]);
// I want to compare the contractInfo before submitting and after submitting.
// I want to see the differences before and after to take a log.
// when I submit log[0] and log[1] are coming with same value.
//Updating Contract table..
$this->getDoctrine()->getManager()->flush();
dump($this->getDoctrine()->getManager()->flush());
$this->addFlash(
'notice', 'Data is Updated.'
);
return $this->redirectToRoute('staff_index');
}
return $this->render('contract/contract_edit.html.twig',
array(
'contractInfo' => $contractInfo,
'form' => $formEdit->createView(),
)
);
}
Solution
In php, objects are passed by reference. Have a look at this small example.
<?php
class A {
public $b = 1;
}
$a = new A();
$b = $a;
$bStill = clone($a);
// when we change $a it will affect $b, but not $bStill, because theres cloned object from previous state of object $a
$a->b = 2;
$c = $a;
// ["b"] => 2
var_dump($b);
// ["b"] => 2
var_dump($c);
// ["b"] => 1
var_dump($bStill);
So for your case, try using $log[0] = clone($contractInfo);
Answered By - Eakethet
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.