表格嵌入多对多在symfony2中多对一。

时间:2012-06-19 11:09:51

标签: php model-view-controller symfony doctrine-orm symfony-forms

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

我有两个名为'地址''AddressType'的实体,它们是相关的,您可以在下面的代码中看到。 我试图做的是当我为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;
    }
}

AddressType实体:

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

    }

我花了整整一天时间坚持这一点,我尝试了很多东西,但是我无法管理让它发挥作用。

感谢任何帮助!

由于

1 个答案:

答案 0 :(得分:0)

编辑表格AddressType: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  
             ));