1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| <?php
// src/AppBundle/Entity/Image.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
use App\Validator\Constraints as AppAssert;
/**
* @Vich\Uploadable
* @ORM\Entity(repositoryClass="AppBundle\Repository\ImageRepository")
* @AppAssert\Image
*/
class Image
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(max=255)
* @var string
*/
private $image;
/**
* @Vich\UploadableField(mapping="images", fileNameProperty="image")
* @Assert\File(
* maxSize="1000k",
* maxSizeMessage="Le fichier excède 1000Ko.",
* mimeTypes={"image/png", "image/jpeg", "image/jpg", "image/svg+xml", "image/gif"},
* mimeTypesMessage= "formats autorisés: png, jpeg, jpg, svg, gif"
* )
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="datetime")
* @var \DateTime
*/
private $updatedAt;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $tmpFile;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Advert", inversedBy="photos")
* @ORM\JoinColumn(nullable=false)
*/
private $advert;
public function __toString(){
return (string) $this->image;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
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;
}
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
}
public function getUpdatedAt(){
return $this->updatedAt;
}
/*
* Set tmpFile
* @return Image
*/
public function setTmpFile($tmpFile)
{
$this->tmpFile = $tmpFile;
return $this;
}
/*
* Get tmpFile
* @return string
*/
public function getTmpFile()
{
return $this->tmpFile;
}
public function getAdvert(): ?Advert
{
return $this->advert;
}
public function setAdvert(?Advert $advert): self
{
$this->advert = $advert;
return $this;
}
public function validate($protocol, Constraint $constraint)
{
if($protocol->getImageFile() === null && $protocol->getImage() === null && $protocol->getUpdatedAt() === null)
{
return $this->context
->buildViolation($constraint->message)
->atPath('imageFile')
->addViolation()
;
}
}
} |
Partager