I want to load all the 'Interns'(Stagiaires) that are to be paid in 'Payment'(Bordereaus).I want to do that by selecting a date interval and passing attributes of Interns through them to select only those who's Internship ends in the date interval.
'Interns' and 'Payment' don't have a relationship but there is a table 'Interns_Payment'(Bordereau_stagiaire) which is linked to both tables. I want to do it through the repository and then load it in the 'edit' of my PaymentController. I'm new to symfnony
I wrote this query in my Payment(Bordereau)Repository
public function findByDate(Bordereau $bordereau){
$query = $this->createQueryBuilder('bo');
return $query->select('bo', 'bord_stag', 'st')
->leftJoin('bo.bordereauStagiaire', 'bord_stag')
->leftJoin('bord_stag.stagiaire', 'st')
->andWhere('st.finStage >= IN(:dateDebut)')
->andWhere('st.finStage <= IN(:dateFin)')
->andWhere('st.isDeleted = IN(:delete)')
->setParameter('delete', false)
->setParameter('dateFin', $bordereau->dateFin)
->setParameter('dateDebut', $bordereau->dateDebut)
->getQuery()
->getResult()
;
}
and this is my Payment(Bordereau) edit function in my controller
#[Route('/{id}/edit', name: 'app_bordereau_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Bordereau $bordereau, BordereauRepository $bordereauRepo): Response
{
$formBordereau = $this->createForm(BordereauEditType::class, $bordereau);
$formBordereau->handleRequest($request);
if ($formBordereau->isSubmitted() && $formBordereau->isValid()) {
$bordereauRepo->save($bordereau, true);
dd($bordereau);
return $this->redirectToRoute('app_bordereau_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('bordereau/edit.html.twig', [
'bordereau' => $bordereau,
'form' => $formBordereau,
]);
}
I was expecting to get the result by checking with a dd($bordereauRepository); but I instead got this result.
I've searched over similar issues concerning Too few parameters in query but could not succeed in finding a solution.
EDIT
I added the setParamaters of dateDebut and dateFin. But I can't access it.
Should I create a custom form of bordereau?
EDIT2
This is my new DQL function
public function findByDate(BordereauEditModel $bordereau, bool $isDeleted = false){
$query = $this->createQueryBuilder('bo');
$query
->select('bo', 'bord_stag', 'st')
->leftJoin('bo.bordereauStagiaire', 'bord_stag')
->leftJoin('bord_stag.stagiaire', 'st')
->andWhere('st.isDeleted = :delete')
->setParameter('delete', $isDeleted);
if ($bordereau->dateDebut) {
$query->andWhere('st.finStage <= :dateDebut')
->setParameter('dateDebut', $bordereau->dateDebut->format('Y-m-d'));
}
if ($bordereau->dateFin) {
$query->andWhere('st.finStage <= :dateFin')
->setParameter('dateFin', $bordereau->dateFin->format('Y-m-d'));
}
$query
->getQuery()
->getResult()
;
}
It prints the result with the static parameters I've set. But with dynamic ones it doesn't.
Also I created a Model BordereauEditModel and a custom form from BordereauEditType
This is my edit function in BordereauController
#[Route('/{id}/edit', name: 'app_bordereau_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Bordereau $bordereau, BordereauRepository $bordereauRepository, BordereauEditModel $bordereauEdit): Response
{
$bordereauEdit = new BordereauEditModel();
$formBordereau = $this->createForm(BordereauEditType::class, $bordereauEdit);
$formBordereau->handleRequest($request);
if ($formBordereau->isSubmitted() && $formBordereau->isValid()) {
$bordereauRepository->findByDate($bordereauEdit);
$bordereauRepository->save($bordereau, true);
dd($bordereauRepository);
return $this->redirectToRoute('app_bordereau_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('bordereau/edit.html.twig', [
'bordereau' => $bordereau,
'form' => $formBordereau,
]);
}
When I try to load the dynamic ones it says 'call to a member function format() on null' . I don't know if this is another issue and I should remove it from here (ie as a paragraph in the main post so that people won't be confused)
Call to a member function format() on null -> Solved
Remove the
INfrom the conditions. If you want to check withIN, you can not use comparison operators.