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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| <?php
namespace Test\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
/**
* Test\MainBundle\Entity\Capture
*
* @ORM\Table(name="test_captures")
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="Test\MainBundle\Entity\CaptureRepository")
*/
class Capture
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Test\MainBundle\Entity\Application", inversedBy="captures")
* @ORM\JoinColumn(name="application_id", referencedColumnName="id", nullable=false)
*/
private $application;
/**
* @var string $image
* @ORM\Column(name="image", type="string", length=255)
*/
private $image;
/**
* @var object $objFile
* @Assert\File( maxSize = "5M", mimeTypesMessage = "Please upload a valid Image")
*/
private $objFile;
public function __construct()
{
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set application
*
* @param Test\MainBundle\Entity\Application $application
* @return Capture
*/
public function setApplication(\Test\MainBundle\Entity\Application $application = null)
{
$this->application = $application;
$this->application->addCapture($this);
return $this;
}
/**
* Get application
*
* @return Test\MainBundle\Entity\Application
*/
public function getApplication()
{
return $this->application;
}
/**
* Set objFile
*
* @param object $objFile
* @return Capture
*/
public function setObjFile($objFile)
{
$this->objFile = $objFile;
return $this;
}
/**
* Get objFile
*
* @return object
*/
public function getObjFile()
{
return $this->objFile;
}
/**
* Set image
*
* @param string $image
* @return Capture
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
// Si le champ d'upload est vide on ne fait rien
if (null === $this->objFile) {
return;
}
// Si on ajoute une image
elseif (null !== $this->objFile) {
$this->image = $this->objFile->getClientOriginalName();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->objFile) {
return;
}
if(!is_dir($this->getUploadRootDir())){
mkdir($this->getUploadRootDir());
}
// vous devez lancer une exception ici si le fichier ne peut pas
// être déplacé afin que l'entité ne soit pas persistée dans la
// base de données comme le fait la méthode move() de UploadedFile
$this->objFile->move($this->getUploadRootDir(), $this->image);
unset($this->objFile);
}
// propriété utilisé temporairement pour la suppression
private $filenameForRemove;
/**
* @ORM\PreRemove()
*/
public function storeFilenameForRemove()
{
$this->filenameForRemove = $this->getUploadRootDir().$this->image;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($this->filenameForRemove) {
unlink($this->filenameForRemove);
rmdir($this->filenameForRemove.'/..');
}
}
protected function getUploadRootDir()
{
// le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
// le document/image dans la vue.
return 'upload/capture_ecran/'.$this->getId().'/';
}
} |
Partager