簡體   English   中英

Symfony FormType錯誤

[英]Symfony FormType Error

創建提供程序時出現錯誤。

傳遞給選擇字段的實體必須進行管理。 也許將它們保留在實體經理中?

我的formType PrestataireType

/**
 * @var bool $admin
 */
private $admin;

/**
 * @var User $user
 */
private $user;

/**
 * DemandeType constructor.
 * @param bool|false $admin
 */
public function __construct($admin = false, $user, $badges = null)
{
    $this->admin = $admin;
    $this->user = $user;
}

/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('files', FileType::class, array(
                'required' => false,
                'attr' => array('class' => 'hidden')
            )
        )
        ->add('name')
        ->add('address')
        ->add('city')
        ->add('zipcode')
        ->add('phone')
        ->add('email')
        ->add('dateStartContract')
        ->add('dateEndContract')
        ->add('frequency')
        ->add('intervention', CollectionType::class, array(
            'entry_type'   => EntityType::class,
            'entry_options'  => array(
                'class'      => 'AppBundle:Intervention',
                'expanded' => false,
                'choice_label' => 'id'
            ),
            'allow_add'    => true,
            'allow_delete' => true,
            'invalid_message' => "Un destinataire n'existe pas",
        ));
    if ($this->user->getResidence() != $this->admin) {
        $builder->add('residence', EntityType::class, array(
            'class' => 'AppBundle:Residence',
            'choices' => $this->user->getResidence(),
        ));
    } else {
        $builder->add('residence', 'entity', array(
            'class' => 'AppBundle:Residence',
            'choice_label' => 'name',
        ));
    }
    ;
}

(實體是具有標識的對象。它們的標識在您的域內具有概念上的含義。在CMS應用程序中,每篇文章都有唯一的ID。您可以通過該ID唯一地標識每篇文章。) 我的實體Prestataire.php

 /**
 * Prestataire
 *
 * @ORM\Table(name="prestataire")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PrestataireRepository")
 */
class Prestataire
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="string", length=255)
     */
    private $address;

    /**
     * @var string
     *
     * @ORM\Column(name="city", type="string", length=255)
     */
    private $city;

    /**
     * @var string
     *
     * @ORM\Column(name="zipcode", type="string", length=255)
     */
    private $zipcode;

    /**
     * @var string
     *
     * @ORM\Column(name="phone", type="string", length=255)
     */
    private $phone;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255)
     */
    private $email;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_start_conttract", type="date")
     */
    private $dateStartContract;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_end_contract", type="date")
     */
    private $dateEndContract;

    /**
     * @var string
     *
     * @ORM\Column(name="frequency", type="string", length=255)
     */
    private $frequency;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_create", type="date")
     */
    private $dateCreate;

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Residence", inversedBy="prestataire")
     * @ORM\JoinTable(name="prestataire_resid",
     *      joinColumns={@ORM\JoinColumn(name="prestataire_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="residence_id", referencedColumnName="id")}
     * )
     */
    private $residence;

    /**
     * Temp file
     * @var File $file
     */
    public $files;

    /**
     * @var
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Upload", cascade={"remove", "persist"})
     */
    private $uploads;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Intervention", mappedBy="prestataire", cascade={"persist","remove"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $intervention;

    /**
     * Prestataire constructor.
     */
    public function __construct()
    {
        $this->residence = new ArrayCollection();
        $this->uploads = new ArrayCollection();
        $this->dateCreate = new \DateTime();
        $this->updatedAt = clone $this->dateCreate;
        $this->intervention = new ArrayCollection();
    }

    function getResidence(){
        return $this->residence;
    }

    /**
     * Add residence
     *
     * @param \AppBundle\Entity\Residence $residence
     *
     * @return Prestataire
     */
    public function addResidence(\AppBundle\Entity\Residence $residence)
    {
        $this->residence[] = $residence;

        return $this;
    }

    /**
     * Remove residence
     *
     * @param \AppBundle\Entity\Residence $residence
     */
    public function removeResidence(\AppBundle\Entity\Residence $residence)
    {
        $this->residence->removeElement($residence);
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Prestataire
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set address
     *
     * @param string $address
     *
     * @return Prestataire
     */
    public function setAddress($address)
    {
        $this->address = $address;

        return $this;
    }

    /**
     * Get address
     *
     * @return string
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * Set city
     *
     * @param string $city
     *
     * @return Prestataire
     */
    public function setCity($city)
    {
        $this->city = $city;

        return $this;
    }

    /**
     * Get city
     *
     * @return string
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set zipcode
     *
     * @param string $zipcode
     *
     * @return Prestataire
     */
    public function setZipcode($zipcode)
    {
        $this->zipcode = $zipcode;

        return $this;
    }

    /**
     * Get zipcode
     *
     * @return string
     */
    public function getZipcode()
    {
        return $this->zipcode;
    }

    /**
     * Set phone
     *
     * @param string $phone
     *
     * @return Prestataire
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * Get phone
     *
     * @return string
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * Set email
     *
     * @param string $email
     *
     * @return Prestataire
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set dateStartContract
     *
     * @param \DateTime $dateStartContract
     *
     * @return Prestataire
     */
    public function setDateStartContract($dateStartContract)
    {
        $this->dateStartContract = $dateStartContract;

        return $this;
    }

    /**
     * Get dateStartContract
     *
     * @return \DateTime
     */
    public function getDateStartContract()
    {
        return $this->dateStartContract;
    }

    /**
     * Set dateEndContract
     *
     * @param \DateTime $dateEndContract
     *
     * @return Prestataire
     */
    public function setDateEndContract($dateEndContract)
    {
        $this->dateEndContract = $dateEndContract;

        return $this;
    }

    /**
     * Get dateEndContract
     *
     * @return \DateTime
     */
    public function getDateEndContract()
    {
        return $this->dateEndContract;
    }

    /**
     * Set frequency
     *
     * @param string $frequency
     *
     * @return Prestataire
     */
    public function setFrequency($frequency)
    {
        $this->frequency = $frequency;

        return $this;
    }

    /**
     * Get frequency
     *
     * @return string
     */
    public function getFrequency()
    {
        return $this->frequency;
    }

    /**
     * Set dateCreate
     *
     * @param \DateTime $dateCreate
     *
     * @return Prestataire
     */
    public function setDateCreate($dateCreate)
    {
        $this->dateCreate = $dateCreate;

        return $this;
    }

    /**
     * Get dateCreate
     *
     * @return \DateTime
     */
    public function getDateCreate()
    {
        return $this->dateCreate;
    }

    /**
     * Add upload
     *
     * @param \AppBundle\Entity\Upload $upload
     *
     * @return Prestataire
     */
    public function addUpload(\AppBundle\Entity\Upload $upload)
    {
        $this->uploads[] = $upload;

        return $this;
    }

    /**
     * Remove upload
     *
     * @param \AppBundle\Entity\Upload $upload
     */
    public function removeUpload(\AppBundle\Entity\Upload $upload)
    {
        $this->uploads->removeElement($upload);
    }

    /**
     * Get uploads
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getUploads()
    {
        return $this->uploads;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return Prestataire
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    /**
     * Add intervention
     *
     * @param \AppBundle\Entity\Intervention $intervention
     *
     * @return Prestataire
     */
    public function addIntervention(\AppBundle\Entity\Intervention $intervention)
    {
        $this->intervention[] = $intervention;

        return $this;
    }

    /**
     * Remove intervention
     *
     * @param \AppBundle\Entity\Intervention $interventions
     */
    public function removeIntervention(\AppBundle\Entity\Intervention $interventions)
    {
        $this->intervention->removeElement($interventions);
    }

    /**
     * Get intervention
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getIntervention()
    {
        return $this->intervention;
    }
}

謝謝你的幫助。

我認為您需要將字段$ intervention設置為實體中的復數形式:

private $interventions;

然后,您必須在FormType中對此進行匹配:

// ...
->add('interventions', CollectionType::class, array(
// ...

並在您的添加/刪除方法中:

public function addIntervention(\AppBundle\Entity\Intervention $intervention)
{
    $this->interventions[] = $intervention;

    return $this;
}

public function removeIntervention(\AppBundle\Entity\Intervention $interventions)
{
    $this->interventions->removeElement($interventions);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM