无法保存集合字段数据

时间:2013-04-09 17:05:59

标签: php symfony doctrine-orm

我无法收集[购买实体]数据

我的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;
}
}

我的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;
}

我做错了什么。

1 个答案:

答案 0 :(得分:1)

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

编辑addPurchase方法,如下所示:

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

    return $this;
}