hello!! je bloque sur l'ajout de mon image (Avatar) à mon commentaire ! ce que je fais ne fonctionne pas!
j'ai une fixture :
namespace App\DataFixtures;
use Faker;
use Faker\Factory;
use App\Entity\User;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use App\Service\UploaderFileServiceInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
class UsersFixtures extends Fixture implements OrderedFixtureInterface
{
private $encoder;
private $uploaderFileService;
private $containerBagInterface;
public function __construct(UserPasswordEncoderInterface $encoder, UploaderFileServiceInterface $uploaderFileService, ContainerBagInterface $containerBagInterface)
{
$this->encoder = $encoder;
$this->uploaderFileService = $uploaderFileService;
$this->containerBagInterface = $containerBagInterface;
}
public function load(ObjectManager $manager)
{
$path = $this->containerBagInterface->get('directory_upload');
$files = glob($path . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
$faker = Faker\Factory::create('fr_FR');
$user = new User(); //creation de mon user role ADMIN
$user->setEmail($faker->email());
$user->setPassword($this->encoder->encodePassword($user, 'bravo'));
$user->setUsername($faker->lastName());
$user->setRoles(['ROLE_ADMINISTRATOR']);
$user->setAvatar($this->getRandomAvatar($faker));
$manager->persist($user);
for ($i = 0; $i <= 5; $i++) {
$user = new User();
$user->setEmail($faker->email());
$user->setPassword($this->encoder->encodePassword($user, 'toto'));
$user->setUsername($faker->lastName());
$user->setAvatar($this->getRandomAvatar($faker));
$user->setRoles(['ROLE_USER']);
$manager->persist($user);
// enregistre l user dans une addReference
$this->addReference('user_' . $i, $user);
}
$manager->flush();
}
private function getRandomAvatar(Faker\Generator $faker): string
{
$listAvatars = [
'avatar_1.jpg',
'avatar_2.jpg',
'avatar_3.jpg',
'avatar_4.jpg',
'avatar_5.jpg',
'avatar_6.jpg',
'avatar_7.jpg',
'avatar_8.jpg',
'avatar_9.jpg',
'avatar_10.jpg',
];
$avatar = $faker->randomElement($listAvatars);
copy(__DIR__ . '/images/' . $avatar, __DIR__ . '/images/copy-' . $avatar);
$newAvatarFileName = 'copy-' .$avatar;
$uploadedFile = new UploadedFile(__DIR__ . '/images/' . $newAvatarFileName, $newAvatarFileName, null, null, true);
return $this->uploaderFileService->upload($uploadedFile);
}
public function getOrder()
{
return 1;
}
}
j'ai donc cree un tableau et mes images sont uploadées dans un dossier upload.
J'ai mes commentaires dans ma fonction Show dans mon controller :
public function show(Request $request, Trick $trick, PaginatorInterface $paginator): Response
{
$comments = $paginator->paginate( // je pagine : si pas de page 1 et oui si page + 1 jusqu' 4
$trick->getComments(),
$request->query->getInt('page', 1), // pour la page 1 si ya page d'autres pages
5
);
$user = $this->getUser();
// creation du commentaire vide
$comment = new Comment();
// generer le formulaire
$commentForm = $this->createForm(CommentType::class, $comment);
// handleRequest pour recuperer de la variable les differents champs
$commentForm->handleRequest($request);
// envoi traitement formulaire :
if ($commentForm->isSubmitted() && $commentForm->isValid()) {
$comment->setTrick($trick);
$comment->setUser($user);
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
// je fais mon flush pour inscrire dans ma db
$em->flush();
$this->addFlash('message', ' Votre message est publié');
return $this->redirectToRoute('trick_index', ['id' => $trick->getId()]);
}
return $this->render('trick/show.html.twig', [
'trick' => $trick,
'commentForm' => $commentForm->createView(),
'comments' => $comments,
]);
}
/**
* @Route("/{id}/edit", name="trick_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Trick $trick): Response
{
$form = $this->createForm(TrickType::class, $trick);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$trick->setUpdateAt(new \DateTime());
$pictures = $trick->getPictures() ;
// faire une boucle pour plusieurs images
foreach ($pictures as $picture) {
if ($picture->getId()!=null){
continue;
}
$filename = $this->uploaderFileService->upload($picture->getFile());
$subtitle = $picture->getSubtitle();
$picture->setName($filename);
$picture->setSubtitle($subtitle);
$this->entityManager->persist($picture);
$trick->addPicture($picture);
}
$this->entityManager->flush();
$this->addFlash(
'success',
'Le trick <strong>' . $trick->getName() . '</strong> a bien eté modifié avec succès.'
);
return $this->redirectToRoute('trick_index');
}
return $this->render('trick/edit.html.twig', [
'trick' => $trick,
'form' => $form->createView(),
]);
}
A chaque fois que je veux ajouter dans ma function show ma function :
et faire une boucle, puisutiliser :
ca ne marche pas
est ce que vous voyez ou je me trompe? peut etre pourrez vous m'aider Merci bien