2013-04-09 91 views
1

我無法收集[採購實體]數據無法保存收集現場數據

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

我在做什麼錯。

+0

什麼是你的問題? – cheesemacfly 2013-04-09 18:45:36

回答

1

您必須手動將PO對象傳遞給購買對象。

編輯addPurchase方法如下:

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

    return $this; 
} 
+0

該部分應該根據數組集合具有語法。 – chirag7jain 2013-04-10 06:18:23

+0

對不起,你的意思是? – 2013-04-10 07:08:34

+0

如果我增加兩個購買到寶。只有一個正在得救。沒有第二次購買的記錄。我想做一些類似於標籤所做的事情http://symfony.com/doc/2.0/cookbook/form/form_collections.html – chirag7jain 2013-04-10 07:20:52