在控制器中调用实体方法

时间:2014-04-14 15:40:17

标签: symfony

我有三个实体:发票,付款和结果

实体之间的关系是: 结果(1,1)-------------(1,n)的发票(1,N)---------------(1,1)付款 这是我的问题:我想在我的PaymentController中创建新的payement时,我会检索Invoice实体,并在同一个PaymentController中创建一个新的Result。

这是我的PaymentController代码:

use MyApp\AccountBundle\Entity\Result;
class PaymentController  extends Controller
  public function createAction()
    {
     $entity  = new Payment();
     $request = $this->getRequest();
     $form    = $this->createForm(new PaymentType(), $entity);
     $form->bindRequest($request);
     $amount=$form->get('amountreceived')->getData();
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getEntityManager();
        $invoice = em->getRepository('MyAppAccountBundle:Invoice')->find($entity->getInvoice()->getId())
        if (!$invoice) {
        throw $this->createNotFoundException('Unable to find Invoice entity.');
            } 
             $result=new Result();
             $result=setDebitAmount($amount);
     $result=setCreditAmount(0);
     $result=setInvoice($invoice);
             $em->persist($result);
             $em->persist($entity);
             $em->flush();
       return $this->redirect($this->generateUrl('payment_show', array('id' =>   $entity->getId())));   
    }
    return $this->render('MyAppAccountBundle:Payment:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView()
    ));

执行PaymentController时(在视图中)我收到错误:  致命错误:在C:\ wamp \ www \ account \ src \ MyApp \ AccountBundle \ Controller \ PaymentController.php中调用未定义的函数MyApp \ AccountBundle \ Controller \ setDebitAmount()...

提前谢谢

1 个答案:

答案 0 :(得分:0)

=更改为->

$result=setDebitAmount($amount);

必须是

$result->setDebitAmount($amount);