chirag7jain
chirag7jain

Reputation: 1547

Can't Save Collection Field Data

I am not able to collection [Purchase Entity] data

My PO Entity --- Create Controller

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(),
    );
}

My Form Builder - 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,))
    ;
}

Purchase Entity

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

My PO Entity

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

What am I doing wrong.

Upvotes: 1

Views: 76

Answers (1)

Vadim Ashikhman
Vadim Ashikhman

Reputation: 10126

You have to pass PO object to Purchase objects manually.

Edit the addPurchase method as below:

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

    return $this;
}

Upvotes: 1

Related Questions