Symfony 表单继承:属性和方法都不存在 [英] Symfony form inheritance : Neither the property nor one of the methods exist
问题描述
我有一个嵌套表单
demand
home
child
godfather
demand
是一个父元素并嵌入了 home
,它嵌入了 child
和 father
(最后两个表单在同级)
demand
is a parent and embed home
which embed child
and father
(2 last forms are on the same level)
在 DemandeType
我有:
$builder
->add('date', 'datetype')
->add('name', 'text')
//...
->add('home', 'home', array(
'mapped' => false,
'data_class' => 'AppBundle\Entity\Home',
'inherit_data' => true
))
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Demand',
));
}
在 HomeType
中:
$builder
->add('address', 'textarea')
//...
->add('child', 'child', array(
'mapped' => false,
'data_class' => 'AppBundle\Entity\Child',
'inherit_data' => true
))
->add('godfather', 'godfather', array(
'mapped' => false,
'data_class' => 'AppBundle\Entity\Godfather',
'inherit_data' => true
))
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Home',
));
}
在 ChildType
和 GodfatherType
中,我只有名字和姓氏的文本字段,以及正确的 data_class.
And in ChildType
and GodfatherType
I have only text fields for firstname, lastname with their right data_class.
但是当我提交表单(DemandType,嵌入所有子表单)时,我收到了这个错误:
But when I submit the form (DemandType, wichh embed all subforms) I got this error :
属性address"和方法getAddress()"、address()"、isAddress()"、hasAddress()"、__get()"都不存在并且在类中具有公共访问权限AppBundle\实体\需求".
Neither the property "address" nor one of the methods "getAddress()", "address()", "isAddress()", "hasAddress()", "__get()" exist and have public access in class "AppBundle\Entity\Demand".
而且这些方法不属于Demand
实体,而是Home
实体.我已经输入了 inherit_data
,我缺少什么?
And these methods don't belong to Demand
entity but Home
entity. I've put the inherit_data
, what I'm missing ?
谢谢
推荐答案
发生这种情况是因为您正在使用 inherit_data
.此属性使表单将整个提交的数据传递给其子项,而不是默认情况下发生的单个属性(或来自 getter 函数的任何内容).
This happens because you're using inherit_data
. This property makes the form pass the entire submitted data to its child instead of a single property (or anything from getter function) which happens by default.
您为 demand
和 home
执行此操作,这就是 home
表单类型接收实例 Demand
实体的原因.所以我猜你想从 home
中删除 inherit_data
并使用:
You do this for both demand
and home
so that's why home
form type receives an instance Demand
entity. So I guess you want to remove inherit_data
from home
and use just:
->add('home', 'home', array(
'mapped' => false,
'data_class' => 'AppBundle\Entity\Home',
))
在这种情况下,home
将从 $demand->getHome()
接收数据,该数据应该是一个 Hone
实体.
In this case home
will receive data from $demand->getHome()
which should be a Hone
entity.
我不确定您是否真的需要使用 inherit_data
,但这取决于您的用例.通常,您不需要它,因为您有实体结构,例如:
I'm not sure you really need to use inherit_data
at all but depends on your use case. Usually, you don't need it because you have structure of entities such as:
/** @ORM\Entity() */
class Demand {
/** @ORM\OneToWhatever() */
private $home;
public function getHome() {
return $this->home;
}
}
/** @ORM\Entity() */
class Home {
/** @ORM\OneToWhatever() */
private $child;
public function getChild() {
return $this->child;
}
}
/** @ORM\Entity() */
class Child { ... }
但我不知道你的数据结构到底是什么,所以很难提供帮助.
But I don't know what exactly your data structure is so it's hard to help.
此外,您正在使用 mapped =>false
我不确定这是你想要的,因为它阻止 Symfony 用表单数据更新实体.
Also, you're using mapped => false
which I'm not sure is what you want because it prevent Symfony from updating entities with form data.
见:
- http://symfony.com/doc/current/参考/forms/types/form.html#mapped
- http://symfony.com/doc/current/reference/forms/types/form.html#inherit-data
这篇关于Symfony 表单继承:属性和方法都不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!