【发布时间】:2014-10-19 16:14:02
【问题描述】:
我对 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;
【问题讨论】:
-
var转储没有手动设置
$bill_id?
标签: php doctrine-orm zend-framework2