Salut, suis actuellement sur la création d'un site web de publication d'événements, j'aimerais que chaque utilisateur sur son interface de gestion ai accès aux événements qu'il a créé j'ai utilisé FOSUserBundle. Et je bloque dessus. Je vous met ci dessous mon controlleur
Voici l'entité User
Et voici l'entité Event
Merci de votre aide je caille actu
edit par Felipe : code placé entre les balises [ code] et [ /code]
Modifié par Felipe (29 May 2019 - 09:38)
<?php
namespace App\Controller\Gestion;
use App\Entity\Event;
use App\Entity\User;
use App\Form\EventType;
use App\Repository\EventRepository;
use Doctrine\Common\Persistence\ObjectManager;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class GestionEventController extends AbstractController
{
/**
* @var EventRepository
*/
private $repository;
/**
* @var ObjectManager
*/
private $manager;
public function __construct(EventRepository $repository, ObjectManager $manager)
{
$this->repository = $repository;
$this->manager = $manager;
}
/**
* @Route("/gestion/event", name="gestion.event.index")
*
* @param PaginatorInterface $paginator
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function index(PaginatorInterface $paginator, Request $request)
{
$user = $this->getUser();
$events = $paginator->paginate(
$this->repository->findAllEvent($user),
$request->query->getInt('page', 1), 10
);
return $this->render('gestion/event/index.html.twig', [
'events' => $events,
'user' => $user,
]);
}
/**
* @Route("/gestion/event/creer", name="gestion.event.new")
*
* @param Request $request
* @param User $user
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function new(Request $request, $id = null)
{
$user = $this->getUser()->getId();
$event = new Event();
$form = $this->createForm(EventType::class, $event, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->manager->persist($event);
$this->manager->flush();
$this->addFlash('success', 'Événement créé avec succès');
return $this->redirectToRoute('gestion.event.index', array('id' => $event->getId()));
}
$user = $this->manager->getRepository('App\Entity\User')->find('id');
return $this->render('gestion/event/new.html.twig', [
'event' => $event,
'user' => $user,
'form' => $form->createView(),
]);
}
/**
* @Route("/gestion/event/{id}", name="gestion.event.delete", methods="DELETE")
*
* @param Event $event
* @param Request $request
*/
public function delete(Event $event, Request $request)
{
if ($this->isCsrfTokenValid('delete'.$event->getId(), $request->get('_token'))) {
$this->manager->remove($event);
$this->manager->flush();
$this->addFlash('success', 'Événement supprimé avec succès ');
}
return $this->redirectToRoute('gestion.event.index');
}
/**
* @Route("/gestion/event/{id}", name="gestion.event.edit")
*
* @param Event $event
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function edit(Event $event, Request $request)
{
$form = $this->createForm(EventType::class, $event);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->manager->flush();
$this->addFlash('success', 'Événement modifié avec succès ');
return $this->redirectToRoute('gestion.event.index');
}
return $this->render('gestion/event/edit.html.twig', [
'event' => $event,
'form' => $form->createView(),
]);
}
}
Voici l'entité User
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ORM\Table(name="utilisateurs")
* @Vich\Uploadable()
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string|null
* @ORM\Column(type="string", length=255)
*/
private $filename;
/**
* @var File
* @Vich\UploadableField(mapping="profile_image", fileNameProperty="filename")
*/
private $imageFile;
/**
* @ORM\Column(type="datetime")
*/
private $updated_at;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Event", inversedBy="users")
*/
private $id_event;
public function __construct()
{
parent::__construct();
$this->id_event = new ArrayCollection();
}
/**
* @return string|null
*/
public function getFilename(): ?string
{
return $this->filename;
}
/**
* @param string|null $filename
* @return User
*/
public function setFilename(?string $filename): User
{
$this->filename = $filename;
return $this;
}
/**
* @return File|null
*/
public function getImageFile(): ?File
{
return $this->imageFile;
}
/**
* @param File|null $imageFile
* @return User
*/
public function setImageFile(?File $imageFile): User
{
$this->imageFile = $imageFile;
if ($this->imageFile instanceof UploadedFile) {
$this->updated_at = new \DateTime('now');
}
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updated_at;
}
public function setUpdatedAt(\DateTimeInterface $updated_at): self
{
$this->updated_at = $updated_at;
return $this;
}
/**
* @return Collection|Event[]
*/
public function getIdEvent(): Collection
{
return $this->id_event;
}
public function addIdEvent(Event $idEvent): self
{
if (!$this->id_event->contains($idEvent)) {
$this->id_event[] = $idEvent;
}
return $this;
}
public function removeIdEvent(Event $idEvent): self
{
if ($this->id_event->contains($idEvent)) {
$this->id_event->removeElement($idEvent);
}
return $this;
}
}
Et voici l'entité Event
<?php
namespace App\Entity;
use Cocur\Slugify\Slugify;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\EventRepository")
* @Vich\Uploadable()
*/
class Event
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $nameEvent;
/**
* @ORM\Column(type="string", length=255)
*/
private $address;
/**
* @ORM\Column(type="datetime")
*/
private $datedebut;
/**
* @ORM\Column(type="datetime")
*/
private $datefin;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $descriptionEvent;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $nameOrganisateur;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $descriptionOrg;
/**
* @ORM\Column(type="boolean")
*/
private $end = false;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="time")
*/
private $hour_d;
/**
* @ORM\Column(type="time")
*/
private $hour_f;
/**
* @var string|null
* @ORM\Column(type="string", length=255)
*/
private $filename;
/**
* @var File|null
* @Vich\UploadableField(mapping="event_image", fileNameProperty="filename")
*/
private $imageFile;
/**
* @ORM\Column(type="datetime")
*/
private $updated_at;
/**
* @ORM\Column(type="string", length=255)
*/
private $city;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Billet", inversedBy="events")
*/
private $billet;
/**
* @ORM\Column(type="string", length=255)
*/
private $category;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="id_event")
*/
private $users;
public function __construct()
{
$this->created_at = new \DateTime();
$this->users = new ArrayCollection();
}
public function getSlug(): string
{
return (new Slugify())->slugify($this->nameEvent);
}
public function getId(): ?int
{
return $this->id;
}
public function getNameEvent(): ?string
{
return $this->nameEvent;
}
public function setNameEvent(string $nameEvent): self
{
$this->nameEvent = $nameEvent;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(string $address): self
{
$this->address = $address;
return $this;
}
public function getDatedebut(): ?\DateTimeInterface
{
return $this->datedebut;
}
public function setDatedebut(\DateTimeInterface $datedebut): self
{
$this->datedebut = $datedebut;
return $this;
}
public function getDatefin(): ?\DateTimeInterface
{
return $this->datefin;
}
public function setDatefin(\DateTimeInterface $datefin): self
{
$this->datefin = $datefin;
return $this;
}
public function getDescriptionEvent(): ?string
{
return $this->descriptionEvent;
}
public function setDescriptionEvent(?string $descriptionEvent): self
{
$this->descriptionEvent = $descriptionEvent;
return $this;
}
public function getNameOrganisateur(): ?string
{
return $this->nameOrganisateur;
}
public function setNameOrganisateur(?string $nameOrganisateur): self
{
$this->nameOrganisateur = $nameOrganisateur;
return $this;
}
public function getDescriptionOrg(): ?string
{
return $this->descriptionOrg;
}
public function setDescriptionOrg(?string $descriptionOrg): self
{
$this->descriptionOrg = $descriptionOrg;
return $this;
}
public function getEnd(): ?bool
{
return $this->end;
}
public function setEnd(bool $end): self
{
$this->end = $end;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getHourD(): ?\DateTimeInterface
{
return $this->hour_d;
}
public function setHourD(\DateTimeInterface $hour_d): self
{
$this->hour_d = $hour_d;
return $this;
}
public function getHourF(): ?\DateTimeInterface
{
return $this->hour_f;
}
public function setHourF(\DateTimeInterface $hour_f): self
{
$this->hour_f = $hour_f;
return $this;
}
/**
* @return mixed
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
/**
* @param mixed $updated_at
* @return Event
*/
public function setUpdatedAt($updated_at)
{
$this->updated_at = $updated_at;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
/**
* @return string|null
*/
public function getFilename(): ?string
{
return $this->filename;
}
/**
* @param string|null $filename
* @return Event
*/
public function setFilename(?string $filename): Event
{
$this->filename = $filename;
return $this;
}
/**
* @return File|null
*/
public function getImageFile(): ?File
{
return $this->imageFile;
}
/**
* @param File|null $imageFile
* @return Event
*/
public function setImageFile(?File $imageFile): Event
{
$this->imageFile = $imageFile;
if ($this->imageFile instanceof UploadedFile) {
$this->updated_at = new \DateTime('now');
}
return $this;
}
public function getBillet(): ?Billet
{
return $this->billet;
}
public function setBillet(?Billet $billet): self
{
$this->billet = $billet;
return $this;
}
public function getCategory(): ?string
{
return $this->category;
}
public function setCategory(string $category): self
{
$this->category = $category;
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->addIdEvent($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
$user->removeIdEvent($this);
}
return $this;
}
}
Merci de votre aide je caille actu
edit par Felipe : code placé entre les balises [ code] et [ /code]
Modifié par Felipe (29 May 2019 - 09:38)