Symfony 表单继承 : Neither the property nor one of the methods exist

标签 symfony symfony-forms

我有一个嵌套表单

demand
    home
       child
       godfather

demand 是父级,并嵌入 home,其中嵌入 childfather (最后 2 个表单位于同一级别)

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',
    ));
}

ChildTypeGodfatherType 中,我只有名字、姓氏及其正确数据类的文本字段。

但是当我提交表单(DemandType,其中嵌入所有子表单)时,我收到此错误:

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,我缺少什么?

谢谢

最佳答案

发生这种情况是因为您正在使用inherit_data。此属性使表单将整个提交的数据传递给其子级,而不是默认情况下发生的单个属性(或来自 getter 函数的任何内容)。

您对 demandhome 都执行此操作,这就是 home 表单类型接收实例 Demand 实体的原因。所以我猜你想从 home 中删除 inherit_data 并仅使用:

->add('home', 'home', array(
    'mapped' => false,
    'data_class' => 'AppBundle\Entity\Home',
))

在这种情况下,home将从$demand->getHome()接收数据,该数据应该是一个Hone实体。

我不确定您是否真的需要使用inherit_data,但这取决于您的用例。通常,您不需要它,因为您具有以下实体结构:

/** @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 { ... }

但我不知道你的数据结构到底是什么,所以很难提供帮助。

此外,您正在使用 mapped => false ,我不确定这是否是您想要的,因为它会阻止 Symfony 使用表单数据更新实体。

参见:

关于Symfony 表单继承 : Neither the property nor one of the methods exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39431263/

相关文章:

forms - 用于多对多关系的 Symfony2 表单小部件

url - symfony2 使用正则表达式为单个 Controller 操作使用多个 url 模式

mysql - Doctrine ,多对多关系问题

php - 如何使用 Goutte 和 Symfony DomCrawler 从样式 = "..."的父 div 中过滤子节点值?

php - Symfony3 : is it possible to change the name of a form?

forms - Symfony2 选择形式 split 成 Twig

symfony - 上传文件不起作用 - vich_uploader

php - 基于 Symfony2 中其他字段值的字段条件验证

php - 操作方法 : Optimize Symfony's forms' performance?

php - Symfony FormType 至少选择了一个 radio