How can I fix this issue "Symfony 5.4: Too few parameters: The query defined 2 parameters but you only bound 1"?
P粉715304239
2023-09-02 12:53:54
<p>I want to load all Stagiaires to be paid in Bordereaus. I want to select only those internships that ended within the date interval by selecting the date intervals and passing the intern's attributes through them. </p><p>
"Interns" and "Payment" are not related, but there is a table "Interns_Payment" (Bordereau_stagiaire) that links to both tables. I want to do this via a repository and then load it into my PaymentController's Edit. I'm new to symfnony</p>
<p>I wrote this query in my Payment(Bordereau)Repository</p>
<pre class="brush:php;toolbar:false;"> 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()
;
}
</pre>
<p>This is the payment (Bordereau) editing function in my controller</p>
<pre class="brush:php;toolbar:false;">
#[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,
]);
}
</pre>
<p>I was hoping to get the result by checking <code>dd($bordereauRepository);</code> but instead I get this result. </p>
<p>I searched for similar issues regarding "Too few parameters in query" but was unsuccessful in finding a solution. </p>
<p><strong>Edit</strong></p>
<p>I added setParamaters for dateDebut and dateFin. But I can't access it.</p>
<p>Should I create a custom form of bordereau? </p>
<p><strong>Edit 2</strong></p>
<p>This is my new DQL function</p>
<pre class="brush:php;toolbar:false;">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()
;
}
</pre>
<p>It prints the results using the static parameters I set. But not for dynamic ones.</p>
<p>I also created a model <code>BordereauEditModel</code> and a custom form</p> from <code>BordereauEditType</code>
<p>This is my <code>edit</code> function</p> in <code>BordereauController</code>
<pre class="brush:php;toolbar:false;"> #[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,
]);
}
</pre>
<p>When I try to load dynamic it says "Call member function format() on null". I don't know if this is another question and I should remove it from here (i.e. as a paragraph in the main post so people don't get confused)</p>
<p>Call member function format() on null -> Solved</p>
Remove
IN
from the condition. If you want to check usingIN
, you cannot use comparison operators.