数据结构:
我\ ExampleBundle \实体\父级:
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 \实体\儿童:
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 \实体\朋友:
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 \实体\学校:
oneToMany:
friends:
targetEntity: My\ExampleBundle\Entity\Friend
mappedBy : school
cascade : ["persist", "remove"]
。
。
我\ ExampleBundle \控制器\ ParentController:
/**
* Edit Action
*/
public function editAction(Request $request, $id)
{
//...
$form = $this->createForm(new ParentType($this->getUser()), $parent);
。
我\ ExampleBundle \表格\型号\ 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 \表格\型号\ 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 \表格\型号\ 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 \表格\型号\ 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 但是,它无法生成隐藏的表单字段。
非常感谢任何帮助或想法。
答案 0 :(得分:0)
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'
));
}