When I create the form in twig it shows only the end_date date-picker.Also when I change the StatisticsType as dateFrom and dateTo it shows that they do not exists in the entity.The purpose is to have two date pickers and one country drop down menu so the request counter filters correctly. Ex. From 01/01/2022 To 27/09/2022 and country Finland the result to be a number.
RequestsNRepository.php `
public function getTotal($dateFrom, $dateTo, $country) : array
{
return $this->createQueryBuilder('a')
->select('count(a.id) as counter')
->join('a.country', 'c')
->andWhere('a.date >= :dateFrom')
->andWhere('a.date <= :dateTo')
->andWhere('c.cName = :country')
->setParameter('dateTo', $dateTo)
->setParameter('dateFrom', $dateFrom)
->setParameter('country', $country)
->getQuery()
->getOneOrNullResult();
}
`
RequestsNController.php `
#[Route('/requestsn')]
class RequestsNController extends AbstractController
{
#[Route('/', name: 'app_requestsN', methods: ['GET', 'POST'])]
public function index(RequestsNRepository $r_repo , Request $request): Response
{
$stat = new RequestsN();
$formStat = $this->createForm(StatisticsType::class, $stat);
$dates = [];
$formStat->handleRequest($request);
if ($formStat->isSubmitted() && $formStat->isValid()) {
$dates = $r_repo->getTotal($formStat['dateFrom']->getData(),$formStat['dateTo']->getData(),$formStat['country']->getData());
$stat = $formStat->getData();
return $this->render('/requestsN/index.html.twig', [
'dates'=> $dates,
'formStat' => $formStat->createView(),
]);
`
StatisticsType `
<?php
namespace App\Form;
use App\Entity\RequestsN;
use App\Entity\WorldCountries;
use Doctrine\ORM\Mapping\Entity;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use App\Repository\RequestsNRepository;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\DateType ;
class StatisticsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('date', DateType::class, [
'widget' => 'single_text',
'html5' => false,
'attr'=>['class'=>'js-datepicker'],
'label' => 'start_date',
'row_attr'=>[
'class'=>'input-daterange'
]
])
->add('date', DateType::class, [
'widget' => 'single_text',
'html5' => false,
'attr'=>['class'=>'js-datepicker'],
'label' => 'end_date',
'row_attr'=>[
'class'=>'input-daterange'
]
])
->add('country', EntityType::class,[
'class'=> worldCountries::class,
'choice_label' => function ( worldCountries $country){
return $country->getcName();
}
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => RequestsN::class,
]);
}
}
`
RequestsN.php Entity
`
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $date = null;
#[ORM\ManyToOne(inversedBy: 'country')]
private ?orldCountries $country = null;
public function getdate(): ?\DateTimeInterface
{
return $this->date;
}
public function setdate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getcountry(): ?worldCountries
{
return $this->country;
}
public function setcountry(?worldCountries $country): self
{
$this->country= $country;
return $this;
}
`