Question

Data Structure:

My\ExampleBundle\Entity\Parent:

oneToMany:
    children:
        targetEntity: Children
        mappedBy    : parent
        cascade     : ["persist", "remove"]
manyToMany:
    friends:
      targetEntity  : Friend
      inversedBy    : parents
      cascade       : ["persist"]
      joinTable     :
        name        : my_parents_and_friends
        joinColumns :
            joinColum:
                name                : parent_id
                referencedColumnName: id
                onDelete            : CASCADE
        inverseJoinColumns:
            joinColum:
                name                : friend_id
                referencedColumnName: id
                onDelete            : CASCADE

.

My\ExampleBundle\Entity\Children:

manyToOne:
    parent:
        targetEntity: Parent
        inversedBy  : children
        joinColumn  :
            name                : parent_id
            referencedColumnName: id
manyToMany:
    friends:
      targetEntity  : Friend
      inversedBy    : children
      cascade       : ["persist"]
      joinTable     :
        name        : my_children_and_friends
        joinColumns :
            joinColum:
                name                : children_id
                referencedColumnName: id
                onDelete            : CASCADE
        inverseJoinColumns:
            joinColum:
                name                : friends_id
                referencedColumnName: id
                onDelete            : CASCADE

.

My\ExampleBundle\Entity\Friend:

manyToOne:
    school:
        targetEntity: My\SchoolBundle\Entity\School
        inversedBy  : friends
        joinColumn  :
            name                : school
            referencedColumnName: id
manyToMany:
    parents:
        targetEntity: Parent
        mappedBy    : friends
    children:
        targetEntity: Children
        mappedBy    : friends

.

My\SchoolBundle\Entity\School:

oneToMany:
    friends:
        targetEntity: My\ExampleBundle\Entity\Friend
        mappedBy    : school
        cascade     : ["persist", "remove"]

.


.

My\ExampleBundle\Controller\ParentController:

/**
 * Edit Action
 */
public function editAction(Request $request, $id)
{
    //...

    $form = $this->createForm(new ParentType($this->getUser()), $parent);

.

My\ExampleBundle\Form\Type\ParentType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    //...

    $builder->add('children', 'collection', array(
        'type' => new ChildrenType($this->user),
        'options' => array(
            'required' => true,
        ),
        'allow_add'    => true,
        'by_reference' => false,
        'allow_delete' => true,
        'prototype'    => true
    ));
}

.

My\ExampleBundle\Form\Type\ChildrenType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    //...

    $builder->add('friends', 'collection', array(
        'type'    => new FriendType($this->user),
        'options' => array(
            'required' => true,
        ),
        'allow_add'    => true,
        'by_reference' => false,
        'allow_delete' => true,
        'prototype'    => true
    ));

.

My\ExampleBundle\Form\Type\FriendType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    //...

    $builder->add('school', 'genemu_jqueryselect2_hidden');

ERROR:
    The form's view data is expected to be of type scalar,
    array or an instance of \ArrayAccess, but is an instance of class My\SchoolBundle\Entity\School.
    You can avoid this error by setting the "data_class" option to "My\SchoolBundle\Entity\School"
    or by adding a view transformer that transforms an instance of class My\SchoolBundle\Entity\School to scalar,
    array or an instance of \ArrayAccess. 

.

Then:
My\ExampleBundle\Form\Type\FriendType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    //...

    $builder->add('school', 'genemu_jqueryselect2_hidden', array(
        'data_class' => 'My\SchoolBundle\Entity\School',
    ));

ERROR:
    ContextErrorException: Catchable Fatal Error: Object of class My\SchoolBundle\Entity\School could not be converted to string
    in /path/to/symfony2/app/cache/dev/twig/b5/df/83d3ad1c70181782da8626f8237b177e7063eb64a745f97ba87b9b8b025d.php line 323

    // View of Twig is simple like this:
    {{ form_widget(friend.school) }}

.

Then:
I do not use the GenemuFormBundle, I tried to create a personally data transformer.

$transformer = new SchoolTransformer($this->entityManager);
$builder->add(
    $builder->create('school',
        'hidden',
        array(
            'by_reference' => false,
            'required'     => false,
            'attr'         => array(
                'class' => 'select2'
            ),
        )
    )->addModelTransformer($transformer)
);

ERROR: This is same error above.

.

So I think usually way is entity field, but I want to apply a select2 for a large amount data.
However, it has failed in generating a hidden field of form.

Any help or ideas would be greatly appreciated.

Was it helpful?

Solution

FriendType do not know how to render the school entity, so he try to call a __toString() method on it, as she's not implemented you get this error.

You can set a property_path attribute to define which field should be rendered. http://symfony.com/doc/master/reference/forms/types/hidden.html#property-path

Something like this should solve your problem :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    //...

    $builder->add('school', 'genemu_jqueryselect2_hidden', array(
        'data_class' => 'My\SchoolBundle\Entity\School',
        'property_path' => 'id'
    ));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top