php - Object is set to null before persisting - ZF2/Doctrine -
php - Object is set to null before persisting - ZF2/Doctrine -
i'm relatively new zend framework 2 , doctrine 2 please bear me.
i have 2 entities - bills , payments. trying create payment form can create payments single bill. problem when go create payment nasty error
catchable fatal error: argument 1 passed application\entity\payment::addbill() must instance of application\entity\bill, null given, called in /var/www/zend/module/bill/src/bill/controller/billcontroller.php
so var dump on $bill_obj
, this:
//var dump results object(application\entity\bill)[337] protected 'inputfilter' => null protected 'id' => int 5 protected 'creditor' => string 'sdafddddddd' (length=11) protected 'type' => string '123fasdfsadfdd' (length=14)
$bill_obj
instance of bill. if set $bill_id
5
works.
public function paymentaction() { $bill_id = (int) $this->params()->fromroute('id'); $bill_obj = $this->getentitymanager()->find('application\entity\bill', $bill_id); var_dump($bill_obj); $form = new paymentform(); $request = $this->getrequest(); if ($request->ispost()) { $payment = new payment(); $payment->addbill($bill_obj); $form->setinputfilter($payment->getinputfilter()); $form->setdata($request->getpost()); if ($form->isvalid()) { $payment->exchangearray($form->getdata()); $this->getentitymanager()->persist($payment); $this->getentitymanager()->flush(); // redirect list of bills homecoming $this->redirect()->toroute('bill'); } } homecoming array('form' => $form); }
and payment entity:
class payment implements inputfilterawareinterface { protected $inputfilter; /** * @orm\id * @orm\column(type="integer"); * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\manytoone(targetentity="bill", inversedby="id") */ protected $bill; /** * @orm\column(type="datetime") */ protected $date; /** * @orm\column(type="float") */ protected $amount; /** * magic getter expose protected properties. * * @param string $property * @return mixed */
bill entity:
class bill implements inputfilterawareinterface { protected $inputfilter; /** * @orm\id * @orm\column(type="integer"); * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\column(type="string") */ protected $creditor; /** * @orm\column(type="string") */ protected $type;
i figured out work-around. set hidden field bill id value , utilize in find method. still know why couldn't id route. if knows please comment. thanks!
public function paymentaction() { $bill_id = (int) $this->params()->fromroute('id'); $form = new paymentform(); $request = $this->getrequest(); if ($request->ispost()) { $data = $request->getpost(); $payment = new payment(); $bill_obj = $this->getentitymanager()->find('application\entity\bill', $data['bill_id']); $payment->addbill($bill_obj); $form->setinputfilter($payment->getinputfilter()); $form->setdata($request->getpost()); if ($form->isvalid()) { $payment->exchangearray($form->getdata()); $this->getentitymanager()->persist($payment); $this->getentitymanager()->flush(); // redirect list of bills homecoming $this->redirect()->toroute('bill'); } } homecoming array('form' => $form, 'id' => $bill_id); }
php doctrine2 zend-framework2
Comments
Post a Comment