symfony 3.4复合形式错误

时间:2018-07-16 23:01:22

标签: php forms symfony

我有4个具有彼此外键的实体:

城市实体:

/** @ORM\Entity(repositoryClass="App\MyBundle\Repository\CityRepository") */
class City
{
    /**
     * @var int
     * @ORM\Id
     */
    private $id;

    /**
     * @var Country
     * @ORM\ManyToOne(targetEntity="Country", inversedBy="cities", fetch="EAGER")
     */
    private $country;

    public function getId(){
        return $this->id;
    }

    /** @return Country  */
    public function getCountry(){
        return $this->country;
    }

    public function setCountry(Country $country){
        $this->country = $country;
        return $this;
    }
}

国家/地区可以包含许多城市:

/** @ORM\Entity(repositoryClass="App\MyBundle\Repository\CountryRepository") */
class Country
{
    /**
     * @var int
     * @ORM\Id
     */
    private $id;

    /**
     * @var ArrayCollection|City[]
     * @ORM\OneToMany(targetEntity="City", mappedBy="country")
     */
    private $cities;

    /**
     * @var Island
     * @ORM\ManyToOne(targetEntity="Island", inversedBy="countries", fetch="EAGER")
     */
    private $island;

    public function getId(){
        return $this->id;
    }

    /** @return ArrayCollection|City[]  */
    public function getCities(){
        return $this->cities;
    }

    /** @return Island */
    public function getIsland(){
        return $this->island;
    }

    public function setIsland(Island $island){
        $this->island = $island;
        return $this;
    }
}

岛屿可以包含许多国家:

/** @ORM\Entity(repositoryClass="App\MyBundle\Repository\IslandRepository") */
class Island
{
    /**
     * @var int
     * @ORM\Id
     */
    private $id;

    /**
     * @var ArrayCollection|Country[]
     * @ORM\OneToMany(targetEntity="Country", mappedBy="island")
     */
    private $countries;

    /**
     * @var Planet
     * @ORM\ManyToOne(targetEntity="Planet", inversedBy="islands", fetch="EAGER")
     */
    private $planet;

    public function getId(){
        return $this->id;
    }

    /**
     * @return ArrayCollection|Country[]
     */
    public function getCountries(){
        return $this->countries;
    }

    /** @return Planet */
    public function getPlanet(){
        return $this->planet;
    }

    public function setPlanet(Planet $planet){
        $this->planet = $planet;
        return $this;
    }
}

Planet可以包含许多岛屿,实体的外观相同。

已创建行星,岛屿和国家,并通过其他界面相互引用。 我需要为City实体构建CityType表单以创建和编辑它(将City移到另一个岛)。 CityType应包含CountryType。国家类型应包含IslandType。 IslandType应该包含PlanetType。这样用户就可以看到所有Planrt-Island-Country-City连锁店。 我为城市创建表单类型:

class CityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('country', CountryType::class);
    }

    public function configureOptions(OptionsResolver $resolver){
        $resolver->setDefaults(['data_class' => City::class]);
    }

    public function getBlockPrefix(){
        return 'city_form_type';
    }
}

然后我为“国家/地区”创建表单类型:

class CountryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        //$builder->add('island', IslandType::class);
    }

    public function configureOptions(OptionsResolver $resolver){
        $resolver->setDefaults([
            'required' => true,
            'class'    => Country::class,
            'compound' => true,
            'query_builder' => function (CountryRepository $repository) {
                return $repository->createQueryBuilder('country');
            }
        ]);
    }

    public function getBlockPrefix(){
        return 'country_form_type';
    }
}

当我在浏览器中检查它时,它就可以工作。但是,当我取消注释 buildForm 方法并为Island创建表单类型时,

class IslandType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        //$builder->add('planet', PlanetType::class);
    }

    public function configureOptions(OptionsResolver $resolver){
        $resolver->setDefaults([
            'required' => true,
            'class'    => Country::class,
            'compound' => true,
            'query_builder' => function (IslandRepository $repository) {
                return $repository->createQueryBuilder('island');
            }
        ]);
    }

    public function getBlockPrefix(){
        return 'island_form_type';
    }
}

我得到错误:预期类型为“对象,数组或空”,“字符串”。 有人可以帮助我了解我在哪里使用错误的类型吗?

0 个答案:

没有答案