Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2.3 many to many form

I'm having some issues getting many to many to work on my form.

My form is laid out in the following way:

User (FOSUserBundle)
Member (custom class)
Region (custom class)

The member form is a subform of the user form, and the region form is a subform of the member form.

Firstly, the error I'm getting is:

Neither the property "region" nor one of the methods "setRegion()", "_set()" or "_call()" exist and have public access in class "Netsite\Bundle\AdminBundle\Entity\Member".

This happens when I submit the form and it hits the:

$form->handleRequest($request);

Here is all the relevant code:

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"]

Entity\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;
    }

Form\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

Entity\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;
    }
}

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

I have no idea what I'm doing wrong.

like image 415
Erekel Avatar asked Dec 10 '25 08:12

Erekel


1 Answers

I think you want to replace in MemberType.php:

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

with

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

This will render an area with checkboxes labeled Region::region (confusing naming scheme btw. should be just 'name'). Multiple checkmarks will be reflected with multiple M2M entries.