Symfony form inheritance : Neither the property nor one of the methods exist

假装没事ソ 提交于 2019-12-24 04:08:09

问题


I have a nested form

demand
    home
       child
       godfather

demand is a parent and embed home which embed child and father (2 last forms are on the same level)

In DemandeType I have:

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

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

And in ChildType and GodfatherType I have only text fields for firstname, lastname with their right data_class.

But when I submit the form (DemandType, wichh embed all subforms) I got this error :

Neither the property "address" nor one of the methods "getAddress()", "address()", "isAddress()", "hasAddress()", "__get()" exist and have public access in class "AppBundle\Entity\Demand".

And these methods don't belong to Demand entity but Home entity. I've put the inherit_data, what I'm missing ?

Thanks


回答1:


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.

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

In this case home will receive data from $demand->getHome() which should be a Hone entity.

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.

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.

See:

  • http://symfony.com/doc/current/reference/forms/types/form.html#mapped
  • http://symfony.com/doc/current/reference/forms/types/form.html#inherit-data


来源:https://stackoverflow.com/questions/39431263/symfony-form-inheritance-neither-the-property-nor-one-of-the-methods-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!