在属性路径“ gouvernorat”中给出的“ string”类型的期望参数“ App \ Entity \ Gouvernorat”

时间:2019-04-02 20:22:58

标签: symfony frameworks

因此,我正在尝试从数据库中填充该下拉列表。我希望下拉列表显示总督的姓名,然后在数据库中插入所选姓名。 我有一个名为Gouvernorat的实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\GouvernoratRepository")
 */
class Gouvernorat
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $nom;

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

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }
}

另一个叫做“停车”:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ParkingRepository")
 */
class Parking
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=55)
     */
    private $libelle;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $gouvernorat;

    /**
     * @ORM\Column(type="string", length=100) 
     */
    private $delegation;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $adresse;

    /**
     * @ORM\Column(type="date")
     */
    private $datemise;

    /**
     * @ORM\Column(type="boolean")
     */
    private $statue;

    /**
     * @ORM\Column(type="integer")
     */
    private $nombreplaces;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\TypeParking")
     * @ORM\JoinColumn(nullable=true)
     */
    private $type;

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

    public function getLibelle(): ?string
    {
        return $this->libelle;
    }

    public function setLibelle(string $libelle): self
    {
        $this->libelle = $libelle;

        return $this;
    }



    public function getDelegation(): ?string
    {
        return $this->delegation;
    }

    public function setDelegation(string $delegation): self
    {
        $this->delegation = $delegation;

        return $this;
    }

    public function getAdresse(): ?string
    {
        return $this->adresse;
    }

    public function setAdresse(string $adresse): self
    {
        $this->adresse = $adresse;

        return $this;
    }

    public function getDatemise(): ?\DateTimeInterface
    {
        return $this->datemise;
    }

    public function setDatemise(\DateTimeInterface $datemise): self
    {
        $this->datemise = $datemise;

        return $this;
    }

    public function getStatue(): ?bool
    {
        return $this->statue;
    }

    public function setStatue(bool $statue): self
    {
        $this->statue = $statue;

        return $this;
    }

    public function getNombreplaces(): ?int
    {
        return $this->nombreplaces;
    }

    public function setNombreplaces(int $nombreplaces): self
    {
        $this->nombreplaces = $nombreplaces;

        return $this;
    }

    public function getType(): ?TypeParking
    {
        return $this->type;
    }

    public function setType(?TypeParking $type): self
    {
        $this->type = $type;

        return $this;
    }
    public function getGouvernorat(): ?string
    {
        return $this->gouvernorat;
    }

    public function setGouvernorat(string $gouvernorat): self
    {
        $this->gouvernorat = $gouvernorat;

        return $this;
    }
}

最后是名为ParkingType.php的表格


<?php

namespace App\Form;

use App\Entity\Parking;
use App\Entity\TypeParking;
use App\Entity\Gouvernorat;

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ParkingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('libelle')


            ->add('gouvernorat', EntityType::class, [
                // looks for choices from this entity
                'class' => Gouvernorat::class,

                // uses the User.username property as the visible option string
                'choice_label' => 'nom',
                'choice_value' => 'nom',
            ])
            //->add('gouvernorat')

            ->add('delegation')
            ->add('adresse')
            ->add('datemise')
            ->add('statue', ChoiceType::class, [
                'choices'  => [
                    'Activé' => true,
                    'Desactivé' => false,
                ],
            ])
            ->add('nombreplaces')
            ->add('type',EntityType::class, [
                'class' => TypeParking::class,
                'choice_label' => 'libelle',
            ])
        ;
    }

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

填充下拉列表就好了,但是每当我尝试提交表单时,都会出现以下错误:


Expected argument of type "string", "App\Entity\Gouvernorat" given at property path "gouvernorat".

1 个答案:

答案 0 :(得分:1)

之所以会这样,是因为表单类型EntityType::class会期望您的实体字段与实体OneToOne中的类GouvernoratParking关系,而不是{您定义的{1}}。

这应该是您正在寻找的解决方案:
string

App\Entity\Parking