Php 隐藏字段:DataTransformer:GenemuFormBundle:Select2:Symfony2中的深度收集表单

Php 隐藏字段:DataTransformer:GenemuFormBundle:Select2:Symfony2中的深度收集表单,php,forms,symfony,symfony-2.3,Php,Forms,Symfony,Symfony 2.3,数据结构: 我的\ExampleBundle\Entity\Parent: oneToMany: children: targetEntity: Children mappedBy : parent cascade : ["persist", "remove"] manyToMany: friends: targetEntity : Friend inversedBy : parents

数据结构:

我的\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

我的\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

我的\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

我的\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);

我的\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. 
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) }}

然后: 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. 
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) }}

然后: 我没有使用GenemuFormBundle,我试图创建一个个人数据转换器

$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.

所以我认为通常的方法是实体字段,但我想对大量数据应用select2。 但是,它无法生成表单的隐藏字段


如果有任何帮助或想法,我们将不胜感激。

FriendType不知道如何渲染学校实体,因此他尝试在其上调用一个uu_u-toString方法,因为她没有实现,因此您会遇到此错误

可以设置property_path属性来定义应该呈现哪个字段。

这样的事情应该可以解决你的问题:

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

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

六羟甲基三聚氰胺六甲醚。。。我明白了。谢谢你的建议。它也不错,我发现了一个不错的代码。https://gist.github.com/bjo3rnf/4061232 此“实体隐藏”代码可能会成功。非常感谢。