对象在持久化之前设置为null-ZF2/原则


Object is set to null before persisting - ZF2/Doctrine

我对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;

我想出了一个变通办法。我设置了一个以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);
}