我对zend框架2和学说2相对较新,所以请耐心等待。
我有两个实体 - 票据和付款。我正在尝试创建一个付款表单,以便我可以向单个帐单付款。问题是,当我去付款时,我得到了一个令人讨厌的错误
可捕获的致命错误:参数1传递给 Application \ Entity \ Payment :: addBill()必须是。的实例 Application \ Entity \ Bill,null给定,调用 /var/www/zend/module/Bill/src/Bill/Controller/BillController.php
所以我在$bill_obj
上进行了var转储并得到了这个:
//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
是Bill的一个实例。如果我将$bill_id
设置为5
则可行。
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 to list of bills
return $this->redirect()->toRoute('bill');
}
}
return array('form' => $form);
}
这是付款实体:
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 to expose protected properties.
*
* @param string $property
* @return mixed
*/
比尔实体:
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;
答案 0 :(得分:0)
我想出了一个解决方法。我设置了一个隐藏字段,其中包含bill id作为值,并在find方法中使用它。我仍然想知道为什么我无法从路线中获取身份证。如果有人知道请评论。谢谢!
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 to list of bills
return $this->redirect()->toRoute('bill');
}
}
return array('form' => $form, 'id' => $bill_id);
}