Upload fichier formulaire
Bonjour,
je suis entrain de developez un site et j'ai un probleme
j'ai deux entité : une qui represente les "sites" et une qui contients les images de chaques sites
voila l'entité "images"
Code:
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
| ?php
namespace MT2\VoteBundle\Entity;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\ORM\Mapping as ORM;
/**
* Image
*
* @ORM\Table(name="image")
* @ORM\Entity(repositoryClass="MT2\VoteBundle\Repository\ImageRepository")
*/
class Image
{
public function __construct()
{
$this->alt ="Banniere";
$this->url = "default.png";
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=255)
*/
private $alt;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set url
*
* @param string $url
*
* @return Image
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set alt
*
* @param string $alt
*
* @return Image
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt
*
* @return string
*/
public function getAlt()
{
return $this->alt;
}
private $file;
public function getFile()
{
return $this->file;
}
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
public function upload()
{
// Si jamais il n'y a pas de fichier (champ facultatif), on ne fait rien
if (null === $this->file) {
return;
}
// On récupère le nom original du fichier de l'internaute
$name = $this->file->getClientOriginalName();
// On déplace le fichier envoyé dans le répertoire de notre choix
$this->file->move($this->getUploadRootDir(), $name);
// On sauvegarde le nom de fichier dans notre attribut $url
$this->url = $name;
// On crée également le futur attribut alt de notre balise <img>
$this->alt = $name;
}
public function getUploadDir()
{
// On retourne le chemin relatif vers l'image pour un navigateur (relatif au répertoire /web donc)
return 'images/upload';
}
protected function getUploadRootDir()
{
// On retourne le chemin relatif vers l'image pour notre code PHP
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
} |
et voila l'entité site
Code:
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
| class Vote
{
public function __construct()
{
$this->name ="TOP MT2";
$this->description = "";
$this->url = "http://topmt2.fr";
$this->images = new Image();
//$this->image = "default.png";
$this->type = "";
$this->nbVote = 0;
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="MT2\VoteBundle\Entity\Image", cascade={"persist"})
*/
private $images;
/**
* @ORM\OneToOne(targetEntity="MT2\VoteBundle\Entity\Premium", cascade={"persist"})
*/
private $premium;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* @var int
*
* @ORM\Column(name="nb_vote", type="integer")
*/
private $nbVote;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Vote
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
*
* @return Vote
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set url
*
* @param string $url
*
* @return Vote
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set nbVote
*
* @param integer $nbVote
*
* @return Vote
*/
public function setNbVote($nbVote)
{
$this->nbVote = $nbVote;
return $this;
}
/**
* Get nbVote
*
* @return int
*/
public function getNbVote()
{
return $this->nbVote;
}
/**
* Set type
*
* @param string $type
*
* @return Vote
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set premium
*
* @param \MT2\VoteBundle\Entity\Premium $premium
*
* @return Vote
*/
public function setPremium(\MT2\VoteBundle\Entity\Premium $premium = null)
{
$this->premium = $premium;
return $this;
}
/**
* Get premium
*
* @return \MT2\VoteBundle\Entity\Premium
*/
public function getPremium()
{
return $this->premium;
}
/**
* Set images
*
* @param \MT2\VoteBundle\Entity\Image $images
*
* @return Vote
*/
public function setImages(\MT2\VoteBundle\Entity\Image $images = null)
{
$this->images = $images;
return $this;
}
/**
* Get images
*
* @return \MT2\VoteBundle\Entity\Image
*/
public function getImages()
{
return $this->images;
}
} |
et voila le controlleur
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| $vote = $user->getVote();
$formBuilder = $this->get('form.factory')->createBuilder(FormType::class, $vote);
$formBuilder
->add('name', TextType::class)
->add('description', TextareaType::class)
->add('url', TextType::class)
->add('type', TextType::class)
->add('save', SubmitType::class)
->add('images', FileType::class)
;
$form = $formBuilder->getForm();
$top = $user->getVote()->getId();
$antispam = $this->services['mt2_vote.antispam'] = new \MT2\VoteBundle\Antispam\MT2Antispam($this->get('doctrine.orm.entity_manager'));
$dayLeft = $antispam->getDayPremium($user->getVote()->getPremium());
if ($request->isMethod('POST'))
{
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($vote);
$em->flush();
}
} |
voila l'erreur que j'ai
http://img4.hostingpics.net/pics/761767uploaddown.png