0

当我尝试在 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';
    }

}

地址类型:

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

    }

我一整天都在坚持这一点,我尝试了很多东西,但我无法让它发挥作用。

任何帮助表示赞赏!

谢谢

4

1 回答 1

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  
             ));
于 2012-06-19T12:07:41.013 回答