1

我无法收集 [Purchase Entity] 数据

我的 PO 实体 --- 创建控制器

public function createAction(Request $request)
{
    $entity  = new PO();
    $form = $this->createForm(new POType(), $entity);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('po_show', array('id' => $entity->getId())));
    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

我的表单生成器 - PO

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('orderno','text',array('attr'=>array
            ('placeholder'=>"Order No",'class'=>'input-medium','pattern'=>'[a-zA-Z0-9]+')))
        ->add('invoiceno','text',array('attr'=>array
            ('placeholder'=>"Invoice No",'class'=>'input-medium','pattern'=>'[a-zA-Z0-9]+')))
        ->add('buyervat','text',array('attr'=>array
            ('placeholder'=>"Buyer VAT No",'class'=>'input-medium')))
        ->add('sellervat','text',array('attr'=>array
            ('placeholder'=>"Seller VAT No",'class'=>'input-medium')))
        ->add('vatamount','number',array('attr'=>array
            ('placeholder'=>"Vat Amount",'class'=>'input-medium','pattern'=>'[0-9]+')))
        ->add('orderdate','date')
        ->add('payment','text',array('attr'=>array
            ('placeholder'=>"Payment Type:Ref No",'class'=>'input-medium',)))
        ->add('credit','number',array('attr'=>array
            ('placeholder'=>"Credit",'class'=>'input-medium',)))
        ->add('note','text',array('attr'=>array
            ('placeholder'=>"Note",'class'=>'input-medium',)))
        ->add('purchases','collection',array(
            'type'=>new PurchaseType(),
            'allow_add'=> true,
            'allow_delete'=>true,
            'by_reference' => false,))
    ;
}

采购实体

<?php

namespace CJ\BusinessBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Purchase
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Purchase
{


/**
 * @ORM\ManyToOne(targetEntity="PO", inversedBy="purchases")
 * @ORM\JoinColumn(name="po_id", referencedColumnName="id")
 */
protected $po;

public function __construct()
{
    $this->setUpdatedAt(new \DateTime());
}

/**
 * Set po
 *
 * @param \CJ\BusinessBundle\Entity\PO $po
 * @return Purchase
 */
public function setPo(\CJ\BusinessBundle\Entity\PO $po = null)
{
    $this->po = $po;

    return $this;
}

/**
 * Get po
 *
 * @return \CJ\BusinessBundle\Entity\PO 
 */
public function getPo()
{
    return $this->po;
}
}

我的采购订单实体

<?php

namespace CJ\BusinessBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * PO
 *
 * @ORM\Table()
 * @ORM\Entity
 */

class PO
{
...

/**
 * @ORM\OneToMany(targetEntity="Purchase", mappedBy="po", cascade={"persist"})
 */
protected $purchases;

public function __construct()
{
    $this->purchases =  new ArrayCollection();
    $this->setUpdatedAt(new \DateTime());
}

/**
 * Add purchases
 *
 * @param \CJ\BusinessBundle\Entity\Purchase $purchases
 * @return PO
 */
public function addPurchase(\CJ\BusinessBundle\Entity\Purchase $purchases)
{
    $this->purchases[] = $purchases;

    return $this;
}

/**
 * Remove purchases
 *
 * @param \CJ\BusinessBundle\Entity\Purchase $purchases
 */
public function removePurchase(\CJ\BusinessBundle\Entity\Purchase $purchases)
{
    $this->purchases->removeElement($purchases);
}

/**
 * Get purchases
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getPurchases()
{
    return $this->purchases;
}

我究竟做错了什么。

4

1 回答 1

1

您必须手动将 PO 对象传递给 Purchase 对象。

编辑addPurchase方法如下:

public function addPurchase(\CJ\BusinessBundle\Entity\Purchase $purchases)
{
    $this->purchases[] = $purchases;
    $purchases->setPo($this);

    return $this;
}
于 2013-04-10T05:26:00.803 回答