Symfony2.3多对多形式

时间:2013-09-10 13:57:41

标签: php forms symfony many-to-many

我有一些问题需要很多人来处理我的表格。

我的表格按以下方式列出:

用户(FOSUserBundle)
会员(定制班)
地区(自定义类)

成员表单是用户表单的子表单,而表单表单是成员表单的子表单。

首先,我得到的错误是:

  

属性“region”和方法“setRegion()”,“_ set()”或“ _call()”之一都不存在,并且在“Netsite \ Bundle”类中具有公共访问权限\ AdminBundle \实体\成员”。

当我提交表单时,会发生这种情况,并且会发出:

$form->handleRequest($request);

以下是所有相关代码:

Member.orm.yml

Netsite\Bundle\AdminBundle\Entity\Member:
type:  entity
table: member
id:
    id:
        type: integer
        generator:
            strategy: AUTO
fields:
    security_question:
        type: string
        length: 255
    security_answer:
        type: string
        length: 255
----- SNIP -----
manyToMany:
    region:
        targetEntity: Region
        cascade: ["persist", "remove"]

实体\ Member.php

<?php

namespace Netsite\Bundle\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Member
 */
class Member
{
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $region;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->region = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add region
     *
     * @param \Netsite\Bundle\AdminBundle\Entity\Region $region
     * @return Member
     */
    public function addRegion(\Netsite\Bundle\AdminBundle\Entity\Region $region)
    {
        $this->region[] = $region;

        return $this;
    }

    /**
     * Remove region
     *
     * @param \Netsite\Bundle\AdminBundle\Entity\Region $region
     */
    public function removeRegion(\Netsite\Bundle\AdminBundle\Entity\Region $region)
    {
        $this->region->removeElement($region);
    }

    /**
     * Get region
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getRegion()
    {
        return $this->region;
    }

表单\ MemberType.php

<?php
namespace Netsite\Bundle\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

class MemberType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('region', new RegionType())

Region.orm.yml

Netsite\Bundle\AdminBundle\Entity\Region:
type:  entity
table: region
id:
    id:
        type: integer
        generator:
            strategy: AUTO
fields:
    region:
        type: string
        length: 255

实体\ Region.php

<?php

namespace Netsite\Bundle\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Region
 */
class Region
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $region;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set region
     *
     * @param string $region
     * @return Region
     */
    public function setRegion($region)
    {
        $this->region = $region;

        return $this;
    }

    /**
     * Get region
     *
     * @return string 
     */
    public function getRegion()
    {
        return $this->region;
    }
}

表单\ RegionType.php

<?php
namespace Netsite\Bundle\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegionType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('region', 'entity', array(
                'class' => 'Netsite\Bundle\AdminBundle\Entity\Region',
                'property' => 'region',
                'empty_data' => null,
                'multiple' => true,
                'expanded' => true,
                'required' => false,
                'label' => false
             ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Netsite\Bundle\AdminBundle\Entity\Region',
            'cascade_validation' => true
        ));
    }

    public function getName() {
        return 'RegionType';
    }
}

我不知道我做错了什么。

1 个答案:

答案 0 :(得分:4)

我想你想要在MemberType.php中替换:

$builder->add('region', new RegionType())

$builder->add('region', 'entity',
              array(
                  'class' => 'YourBundle:Region',
                  'multiple' => true,
                  'expanded' => true,
                  'required' => true|false,
                  'property' => 'region',
                  'label' => 'your.label',
                  'error_bubbling' => true,
              )
)

这将呈现一个区域,其复选框标记为Region :: region(令人困惑的命名方案btw。应该只是'name')。多个M2M条目将反映多个复选标记。