簡體   English   中英

隱藏字段:DataTransformer:GenemuFormBundle:Select2:Symfony2中的深層收集表單

[英]hidden field : DataTransformer : GenemuFormBundle : Select2 : Deep Collection Form in Symfony2

數據結構:

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


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

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

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

然后:
我的\\ 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) }}

然后:
我不使用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不知道如何呈現學校實體,因此他嘗試在其上調用__toString()方法,因為未實現該錯誤,因此您沒有實現。

您可以設置property_path屬性來定義應呈現的字段。 http://symfony.com/doc/master/reference/forms/types/hidden.html#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'
    ));
}

暫無
暫無

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

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