1

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;
    }

`

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Rene
  • 11
  • 2
  • "it shows only the end_date date-picker" - what have you tried to resolve that? Maybe it could help to use distinct names for the form fields? Having a field called `date` twice will obviously cause problems – Nico Haase Sep 27 '22 at 07:28
  • I tried by using dateFrom and dateTo instead of date but is says "Can't get a way to read the property "dateFrom" in class "App\Entity\RequestsN"." – Rene Sep 27 '22 at 07:34
  • As you haven't shared that class `RequestsN`, it's difficult to tell you why that error occurs. But I would assume that the error message is self-explanatory? – Nico Haase Sep 27 '22 at 07:40
  • This is the entity class, It's the last one. The problem is that my entity has date and country inside and I want to split date in two parts. "dateFrom" and "dateTo". – Rene Sep 27 '22 at 07:48
  • And, does it contain a `dateFrom` or `dateTo` property? Why do you directly map the `StatisticsType` to that entity if it does not match the fields you need? – Nico Haase Sep 27 '22 at 07:58
  • I created a custom form in the twig file but I faced problem with the country. So I thought it would work if I make a form for both. Is there any different way that I can handle StatisticsType form instead of matching it with the entity? – Rene Sep 27 '22 at 08:09
  • Yeah: use a different model for that, decoupled from your database entity – Nico Haase Sep 27 '22 at 08:12

0 Answers0