簡體   English   中英

在symfony2中嵌入了很多對很多和一對多的表單。

[英]Form embedded for many to many & many to one in 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;
    }
}

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

    }

我花了整整一天的時間堅持這樣做,我嘗試了很多事情,但是我無法設法使其正常工作。

任何幫助表示贊賞!

謝謝

編輯表格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  
             ));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM