Php 对象在持久化之前设置为null-ZF2/doctor
我对zend框架2和条令2比较陌生,所以请耐心听我说 我有两个实体——账单和付款。我正在尝试创建一个付款表单,以便我可以对单个账单进行付款。问题是,当我去付款时,我犯了一个严重的错误 可捕获致命错误:参数1传递给 Application\Entity\Payment::addBill()必须是的实例 Application\Entity\Bill,给定为空,在中调用 /var/www/zend/module/Bill/src/Bill/Controller/BillController.php 因此,我在Php 对象在持久化之前设置为null-ZF2/doctor,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,我对zend框架2和条令2比较陌生,所以请耐心听我说 我有两个实体——账单和付款。我正在尝试创建一个付款表单,以便我可以对单个账单进行付款。问题是,当我去付款时,我犯了一个严重的错误 可捕获致命错误:参数1传递给 Application\Entity\Payment::addBill()必须是的实例 Application\Entity\Bill,给定为空,在中调用 /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\u obj
是bill的一个实例。如果我将$bill\u 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;
我想出了一个解决办法。我设置了一个以billid为值的隐藏字段,并在find方法中使用它。我仍然想知道为什么我不能从路线上获得id。如果有人知道,请评论。谢谢
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);
}
var转储没有手动设置
$bill\u id
?