简体   繁体   中英

Form embedded for many to many & many to one in symfony2.

I have a problem when I try to embed a form in Symfony2 for many to many relations or many to one relations.

I have two entities called 'Address' and 'AddressType' and they are related as you can see on code below. What I tried to do is when I created a form for Address, I embedded the form for AddressType. I've already tried embedding a collection of AddressType to Address form, but when I try to embed the result of this to Address it seems not to work.

Address Entity

namespace Webmuch\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @ORM\Entity
 */
class Address
{
    protected $id;
    protected $line1;
    protected $line2;
    protected $state;
    protected $city;
    protected $zip;
    protected $country;
    protected $phone;

    /**
     * @ORM\ManyToOne(targetEntity="AddressType")
     * @ORM\JoinColumn(name="address_type_id", referencedColumnName="id")
     */
    protected $type;

    public function __construct()
    {
        $this->type = new ArrayCollection();
    }

    /**
     * Set type
     *
     * @param Webmuch\ProductBundle\Entity\AddressType $type
     */
    public function setType(\Webmuch\ProductBundle\Entity\AddressType $type)
    {
        $this->type = $type;
    }

    /**
     * Get type
     *
     * @return Webmuch\ProductBundle\Entity\AddressType 
     */
    public function getType()
    {
        return $this->type;
    }
}

AddressType Entity:

namespace Webmuch\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class AddressType
{

    protected $id;

    protected $title;

    public function __construct()
    {
        $this->title = false;
    }


}

In form section->

form

AddressType:

namespace Webmuch\AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class AddressType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('line1')
            ->add('line2')
            ->add('city')
            ->add('zip')
            ->add('country')
            ->add('phone')
        ->add('type','collection', array( 'type' =>  new AddressTypeType(),
                                              'allow_add' => true,
                                              'prototype' => true,
                                              'by_reference' => false,
                                              ));
    }
    public function getDefaultOptions(array $options)
    {
        return array('data_class' => 'Webmuch\ProductBundle\Entity\Address');
    }

    public function getName()
    {
        return 'address';
    }

}

AddressTypeType:

namespace Webmuch\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class AddressTypeType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('title');
        ;
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Webmuch\ProductBundle\Entity\AddressType',
        );
    }

    public function getName()
    {
        return 'addresstypetype';
    }

}

Controller Section->

namespace Webmuch\AdminBundle\Controller;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
    use Webmuch\ProductBundle\Entity\Address;
    use Webmuch\AdminBundle\Form\AddressType;

    /**
     * Address controller.
     *
     * @Route("/cusadmin/address")
     */
    class AddressController extends Controller
    {
         /**
     * Displays a form to create a new Address entity.
     *
     * @Route("/new", name="admin_address_new")
     * @Template()
     */
        public function newAction()
        {
            $entity = new Address();

            $form   = $this->createForm(new AddressType(), $entity);

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

    }

I've spent the whole day stuck with this and I have tried a lot of things but I couldn't manage get it working.

Any help is appreciated!

Thanks

Edit form AddressType: nd write this code,may be this is help full....

 public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('line1')
            ->add('line2')
            ->add('city')
            ->add('zip')
            ->add('country')
            ->add('phone')
            ->add('type','entity', array('class'=>'WebmuchProductBundle:AddressType','property'=>'value','multiple'=>true  
             ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM