1

我有一个问题,我在我的网站上使用 EasyAdminBundle 和 VichUploaderBundle。令人担忧的是,当我删除图像时,它会返回错误,如下所示:

违反完整性约束:1048 Le champ 'image' ne peut être vide (null)

这是我的代码:

配置.yml:

vich_uploader:
db_driver: orm
mappings:
        photo_images:
            uri_prefix:         %app.path.photo_images%
            upload_destination: %kernel.root_dir%/../web/uploads/images
            namer:              vich_uploader.namer_uniqid

我的easyadmin配置:

entities:
    Event:
        class: AppBundle\Entity\Event
        label: 'Evenements'
        new:
            title: 'Création événement'
            fields: 
                - { property: 'nom', label: 'Nom/Intitulé' }
                - { property: 'date', label: 'Date' }
                - { property: 'heure', label: 'Heure' }
                - { property: 'lieu', label: 'Lieu' }
                - { property: 'categorie', label: 'Catégorie' }
                - { property: 'prix', label: 'Prix' }
                - { property: 'imageFile', label: 'Image',type: 'vich_image' }
                - { property: 'isActive', label: 'Publié' }
        edit:
            title: 'Evenement (#%%entity_id%%)'
            fields: 
                - { property: 'nom', label: 'Nom/Intitulé' }
                - { property: 'date', label: 'Date' }
                - { property: 'heure', label: 'Heure' }
                - { property: 'lieu', label: 'Lieu' }
                - { property: 'categorie', label: 'Catégorie' }
                - { property: 'prix', label: 'Prix' }
                - { property: 'imageFile', label: 'Image', type: 'vich_image' }
                - { property: 'isActive', label: 'Publié' }

我的实体:

<?php

 namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;
 use Symfony\Component\HttpFoundation\File\File;
 use Vich\UploaderBundle\Mapping\Annotation as Vich;

 /**
 * Events
 *
 * @ORM\Table(name="event")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\EventRepository")
 * @Vich\Uploadable
 */
 class Event
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 * @Assert\NotBlank(message = "not null")
 * @ORM\Column(name="nom", type="string", length=255)
 */
private $nom;

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

/**
 * @var \Time
 *
 * @ORM\Column(name="heure", type="time")
 */
private $heure;

/**
 * @var string
 * @Assert\NotBlank(message = "not null")
 * @ORM\Column(name="lieu", type="string", length=255)
 */
private $lieu;

/**
 * @var string
 * @Assert\NotBlank(message = "not null")
 * @ORM\Column(name="categorie", type="string", length=255)
 */
private $categorie;

/**
 * @var float
 * @ORM\Column(name="prix", type="float")
 */
private $prix;

/**
 * @var bool
 * @ORM\Column(name="isActive", type="boolean", options={"default":"0"})
 */
protected $isActive;

/**
* One Event has Many Command.
* @ORM\OneToMany(targetEntity="Commande", mappedBy="event")
*/
private $commandes;

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

/**
 * @Vich\UploadableField(mapping="photo_images", fileNameProperty="image")
 * @var File
 */
private $imageFile;

/**
 * @ORM\Column(type="datetime")
 * @var \DateTime
 */
private $updatedAt;

public function __toString()
{
    return $this->nom;
}

public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    // VERY IMPORTANT:
    // It is required that at least one field changes if you are using Doctrine,
    // otherwise the event listeners won't be called and the file is lost
    if ($image) {
        // if 'updatedAt' is not defined in your entity, use another property
        $this->updatedAt = new \DateTime('now');
    }
}

public function getImageFile()
{
    return $this->imageFile;
}

public function setImage($image)
{
    $this->image = $image;
}

public function getImage()
{
    return $this->image;
}

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



/**
 * Set nom
 *
 * @param string $nom
 *
 * @return Event
 */
public function setNom($nom)
{
    $this->nom = $nom;

    return $this;
}

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

/**
 * Set date
 *
 * @param \DateTime $date
 *
 * @return Event
 */
public function setDate($date)
{
    $this->date = $date;

    return $this;
}

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

/**
 * Set heure
 *
 * @param \DateTime $heure
 *
 * @return Event
 */
public function setHeure($heure)
{
    $this->heure = $heure;

    return $this;
}

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

/**
 * Set lieu
 *
 * @param string $lieu
 *
 * @return Event
 */
public function setLieu($lieu)
{
    $this->lieu = $lieu;

    return $this;
}

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

/**
 * Set categorie
 *
 * @param string $categorie
 *
 * @return Event
 */
public function setCategorie($categorie)
{
    $this->categorie = $categorie;

    return $this;
}

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

/**
 * Set prix
 *
 * @param float $prix
 *
 * @return Event
 */
public function setPrix($prix)
{
    $this->prix = $prix;

    return $this;
}

/**
 * Get prix
 *
 * @return float
 */
public function getPrix()
{
    return $this->prix;
}

/**
 * Set isActive
 *
 * @param boolean $isActive
 *
 * @return Event
 */
public function setIsActive($isActive)
{
    $this->isActive = $isActive;

    return $this;
}

/**
 * Get isActive
 *
 * @return boolean
 */
public function getIsActive()
{
    return $this->isActive;
}
/**
 * Constructor
 */
public function __construct()
{
    $this->commandes = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add commande
 *
 * @param \AppBundle\Entity\Commande $commande
 *
 * @return Event
 */
public function addCommande(\AppBundle\Entity\Commande $commande)
{
    $this->commandes[] = $commande;

    return $this;
}

/**
 * Remove commande
 *
 * @param \AppBundle\Entity\Commande $commande
 */
public function removeCommande(\AppBundle\Entity\Commande $commande)
{
    $this->commandes->removeElement($commande);
}

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

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

    return $this;
}

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

我不明白错误可能来自哪里,或者我做错了什么。我按照本网站上的教程进行操作:链接

谢谢您的回答

4

1 回答 1

4

在您的实体事件nullable=true中添加:

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 * @var string
 */
private $image;
于 2018-02-27T05:39:39.413 回答