Bonjour à tous,
Je suis encore débutant sur Symfony 4.3 mais j'adore, je me tire les cheuveux mais c'est un réel kiff. Voici mon problème.
Je souhaite créer une session pédagogique, dans cette session je peu créer des feuilles de présence. Mon but est de savoir si j'ai une ou plusieur feuille de présence présente dans ma sessions. Rien de plus simple me direz vous, mais pas pour moi ^^.
J'ai donc créer une entité pour mes feuilles de présence nommé "Timesheet" et une entité pour mes sessions nommée "Sessions".
Mon timesheet est en relation ManyToOne pour mes sessions et mes sessions en OneToMany pour mes timesheet. Je pense ne pas me tromper la dessus sauf si j'ai rien compris au relation ;-)
Dans ma session je souhaite vérifier si une feuille de présence est donc créer et voici ce que je fait en premier mon controller de session :
/**
* @Route("/reprendre-session-pedagogique/", name="session.edit", methods="POST")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function edit(Request $request)
{
$idSession = $request->request->get('_id');
$session = $this->getDoctrine()
->getRepository(Timesheet::class)
->findByIdSessions($idSession);
var_dump($session);
return $this->render('front/management/session/list-action.html.twig', [
'idSession' => $idSession
]);
}
En second mon entity Sessions :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\SessionsRepository")
*/
class Sessions
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\School")
* @ORM\JoinColumn(nullable=false)
*/
private $Schools;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Classroom")
* @ORM\JoinColumn(nullable=false)
*/
private $Classrooms;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Teacher")
* @ORM\JoinColumn(nullable=false)
*/
private $Teachers;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(nullable=false)
*/
private $Users;
/**
* @ORM\Column(type="date")
*/
private $date;
/**
* @ORM\Column(type="time")
*/
private $timesheet;
/**
* @ORM\Column(type="date")
*/
private $dateStart;
/**
* @ORM\Column(type="date")
*/
private $dateEnd;
/**
* @ORM\Column(type="boolean")
*/
private $open = true;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
*/
private $updateAt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Timesheet", mappedBy="Sessions")
*/
private $timesheets;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->updateAt = new \DateTime('now');
$this->timesheets = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getSchools(): ?School
{
return $this->Schools;
}
public function setSchools(?School $Schools): self
{
$this->Schools = $Schools;
return $this;
}
public function getClassrooms(): ?Classroom
{
return $this->Classrooms;
}
public function setClassrooms(?Classroom $Classrooms): self
{
$this->Classrooms = $Classrooms;
return $this;
}
public function getTeachers(): ?Teacher
{
return $this->Teachers;
}
public function setTeachers(?Teacher $Teachers): self
{
$this->Teachers = $Teachers;
return $this;
}
public function getUsers(): ?User
{
return $this->Users;
}
public function setUsers(?User $Users): self
{
$this->Users = $Users;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getTimesheet(): ?\DateTimeInterface
{
return $this->timesheet;
}
public function setTimesheet(\DateTimeInterface $timesheet): self
{
$this->timesheet = $timesheet;
return $this;
}
public function getDateStart(): ?\DateTimeInterface
{
return $this->dateStart;
}
public function setDateStart(\DateTimeInterface $dateStart): self
{
$this->dateStart = $dateStart;
return $this;
}
public function getDateEnd(): ?\DateTimeInterface
{
return $this->dateEnd;
}
public function setDateEnd(\DateTimeInterface $dateEnd): self
{
$this->dateEnd = $dateEnd;
return $this;
}
public function getOpen(): ?bool
{
return $this->open;
}
public function setOpen(bool $open): self
{
$this->open = $open;
return $this;
}
public function getDateStartSession(): ?\DateTimeInterface
{
return $this->dateStartSession;
}
public function setDateStartSession(\DateTimeInterface $dateStartSession): self
{
$this->dateStartSession = $dateStartSession;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdateAt(): ?\DateTimeInterface
{
return $this->updateAt;
}
public function setUpdateAt(\DateTimeInterface $updateAt): self
{
$this->updateAt = $updateAt;
return $this;
}
/**
* @return Collection|Timesheet[]
*/
public function getTimesheets(): Collection
{
return $this->timesheets;
}
public function addTimesheet(Timesheet $timesheet): self
{
if (!$this->timesheets->contains($timesheet)) {
$this->timesheets[] = $timesheet;
$timesheet->setSessions($this);
}
return $this;
}
public function removeTimesheet(Timesheet $timesheet): self
{
if ($this->timesheets->contains($timesheet)) {
$this->timesheets->removeElement($timesheet);
// set the owning side to null (unless already changed)
if ($timesheet->getSessions() === $this) {
$timesheet->setSessions(null);
}
}
return $this;
}
}
puis mon entity Timesheet :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\TimesheetRepository")
*/
class Timesheet
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="boolean")
*/
private $presence;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $createdAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\School")
* @ORM\JoinColumn(nullable=false)
*/
private $Schools;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Classroom")
* @ORM\JoinColumn(nullable=false)
*/
private $Classrooms;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Student", inversedBy="timesheets")
*/
private $Students;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Sessions", inversedBy="timesheets")
* @ORM\JoinColumn(nullable=false)
*/
private $Sessions;
public function __construct()
{
$this->createdAt = new \DateTime('now');
$this->Students = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getPresence(): ?bool
{
return $this->presence;
}
public function setPresence(bool $presence): self
{
$this->presence = $presence;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getSchools(): ?School
{
return $this->Schools;
}
public function setSchools(?School $Schools): self
{
$this->Schools = $Schools;
return $this;
}
public function getClassrooms(): ?Classroom
{
return $this->Classrooms;
}
public function setClassrooms(?Classroom $Classrooms): self
{
$this->Classrooms = $Classrooms;
return $this;
}
/**
* @return Collection|Student[]
*/
public function getStudents(): Collection
{
return $this->Students;
}
public function addStudent(Student $student): self
{
if (!$this->Students->contains($student)) {
$this->Students[] = $student;
}
return $this;
}
public function removeStudent(Student $student): self
{
if ($this->Students->contains($student)) {
$this->Students->removeElement($student);
}
return $this;
}
public function getSessions(): ?Sessions
{
return $this->Sessions;
}
public function setSessions(?Sessions $Sessions): self
{
$this->Sessions = $Sessions;
return $this;
}
}
Puis mon repo Timesheet :
<?php
namespace App\Repository;
use App\Entity\Timesheet;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @method Timesheet|null find($id, $lockMode = null, $lockVersion = null)
* @method Timesheet|null findOneBy(array $criteria, array $orderBy = null)
* @method Timesheet[] findAll()
* @method Timesheet[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TimesheetRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Timesheet::class);
}
// /**
// * @return Timesheet[] Returns an array of Timesheet objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('t')
->andWhere('t.exampleField = :val')
->setParameter('val', $value)
->orderBy('t.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
public function findByIdSessions($id)
{
$entityManager = $this->getEntityManager();
$query = $entityManager->createQuery(
'SELECT t, s
FROM App\Entity\Timesheet t
INNER JOIN t.sessions_id s
WHERE t.id = :id'
)->setParameter('id', $id);
return $query->getOneOrNullResult();
}
/*
public function findOneBySomeField($value): ?Timesheet
{
return $this->createQueryBuilder('t')
->andWhere('t.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
Pour autant j'obtiens cette erreur :
[Semantical Error] line 0, col 89 near 's
': Error: Class App\Entity\Timesheet has no association named sessions_id
Je comprend bien qu'il me dit qu'il ne connait aucune relation entre mon Timesheet et ma session mais pour autant il me semble l'avoir fait ou mal ^^
J'aurais besoins d'explication svp.
Merci à vous tous.
Problème résolu, voici comment j'ai procédé :
public function edit(Request $request)
{
$idSession = $request->request->get('_id');
$user = $this->getUser();
$repository = $this->getDoctrine()->getRepository(Timesheet::class);
$userSession = $repository->findBy(
['Sessions' => $idSession]
);
return $this->render('front/management/session/list-action.html.twig', [
'idSession' => $idSession,
'sessions' => count($userSession)
]);
}
Merci à tous
Bonjour,
tu es sur de ça ?
$idSession = $request->request->get('_id');
C'est pas plutot ?
$idSession = $request->request->get('id');
Oui je passe l'id en methode post comme ceci :
<form method="POST" action="{{ path('session.edit') }}" style="display: inline-block;">
<input type="hidden" name="_id" value="{{ session.id }}">
<button class="sessions-card__btn btn btn-add">Reprendre la session</button>
</form>
Pour autant je n'arrive toujours pas a recuperer la liste de feulilles de présence correspondante à l'id de ma session.