在Symfony中提交之前和之后,如何比较表格的价值?

时间:2018-06-25 06:27:15

标签: php symfony

我正在使用symfony 3.4.8版本。当我更新表单数据时,我想查看提交之前和提交之后的差异。

在$ formEdit-> handleRequest($ request)之后;行的旧值正在更改为新值,但我不想要它。

但是我做不到。

这是我的控制者。

/**
 * @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(),
        )
    );
}

3 个答案:

答案 0 :(得分:1)

在php中,对象是通过引用传递的。看看这个小例子。

<?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);

因此,对于您的情况,请尝试使用$log[0] = clone($contractInfo);

答案 1 :(得分:1)

在从表单刷新实体之前,您应该只检查对象(通过克隆对象)

public function functionToCompare($original, $new)
{
    return true;
}

/**
 * @Route("/contract/{contractId}/Edit", name="contract_edit")
 */
public function editContract(Request $request, $contractId)
{

    $contractInfo = $this->getDoctrine()->getRepository('AppBundle:myContract\Contract')->find($contractId);

    $formEdit = $this->createForm(ContractEditType::class, $contractInfo);

    $formEdit->handleRequest($request);

    if ($formEdit->isSubmitted() && $formEdit->isValid()) {

        $oldContractInfo = clone $contractInfo; // save old entity

        $contractInfo = $form->getData(); // update entity from form

        $this->functionToCompare($oldContractInfo, $contractInfo);

        //Updating Contract table..
        $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(),
        )
    );
}

答案 2 :(得分:0)

对于Symfony 2,您必须在处理请求对象之前克隆实体。 如果在处理请求对象之后放置克隆,则将克隆更改的值

$oldContractInfo = clone $contractInfo; // save old entity
$formEdit->handleRequest($request);
if ($formEdit->isSubmitted() && $formEdit->isValid()) {
    $contractInfo = $form->getData(); // update entity from form
}