2012-06-19 35 views
0

当我尝试在Symfony2中嵌入一个表单以进行多对多关系或多对一关系时,遇到问题。在symfony2中嵌入多对多和多对多的表单。

我有两个实体称为“地址”“地址类型”,正如你可以在下面的代码看看它们是相关的。 我试图做的是当我为Address创建表单时,我嵌入了AddressType的表单。我已经尝试将AddressType的一个集合嵌入到Address表单中,但是当我尝试将此结果嵌入到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'; 
    } 

}

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

}

控制器中─>

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

    } 

我花了整整一天坚持了这一点,我已经尝试了很多事情,但我无法管理得到它的工作。

任何帮助表示赞赏!

感谢

回答

0

编辑表格地址类型: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 
      )); 
+0

我试试这个代码,但它的答案仍无法正常工作的感谢。 –

+0

看到这个链接..http://stackoverflow.com/questions/9740529/symfony2-dynamic-form-generation – Sid