【问题标题】:Object is set to null before persisting - ZF2/Doctrine对象在持久化之前设置为 null - ZF2/Doctrine
【发布时间】: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


【解决方案1】:

我想出了一个解决方法。我设置了一个以账单 ID 作为值的隐藏字段,并在 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多