在symfony2中嵌入多对多和多对一的表单


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

当我尝试在Symfony2中为多对多关系或多对一关系嵌入表单时,我遇到了问题。

我有两个名为"地址"和"地址类型"的实体,它们是相关的,正如您在下面的代码中看到的那样。我尝试做的是,当我为地址创建表单时,我嵌入了地址类型的表单。我已经尝试嵌入 AddressType to Address 表单的集合,但是当我尝试将此结果嵌入到地址时,它似乎不起作用。

地址实体

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

地址类型实体:

namespace Webmuch'ProductBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * @ORM'Entity
 */
class AddressType
{
    protected $id;
    protected $title;
    public function __construct()
    {
        $this->title = false;
    }

}

在表单部分>

形式

地址类型:

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

}

地址类型类型:

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

}

控制器部分->

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()
            );
        }
    }
我花了

一整天的时间坚持这个问题,我尝试了很多事情,但我无法让它工作。

任何帮助不胜感激!

谢谢

编辑表单 地址类型: nd 写这个代码,可能是这个帮助全....

 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  
             ));