Search code examples
phpdatetimedoctrine-ormformbuilder

Comparing dates results with Doctrine


I wrote an api call with form builder which creates two date fields. The idea is to choose two dates and return results from db of data in that date range.

I can't find where the problem is but I think the syntax in query builder is not right.

I don't get any errors just an empty array.

Here is an api call from service

public function getTransaction()
{
    $result = $this->getTransactionRepository()
        ->createQueryBuilder('a')
        ->select('a')
        ->where('a.date >= :from')
        ->andWhere('a.date <= :to')
        ->setParameter('from', $date->format('Y-m-d H:i:s'))
        ->setParameter('to',   $date->format('Y-m-d H:i:s'))
        ->orderBy('p.id')
        ->getQuery()
        ->getArrayResult();

    return $result;
}

My controller

$form = $this->createFormBuilder()
        ->add('dateFrom', DateType::class, array(
            'widget' => 'single_text',
            'attr' => array(
                'dateFrom' => (new \DateTime())->format('Y-m-d'),
            )))
        ->add('dateTo', DateType::class, array(
            'widget' => 'single_text',
            'attr' => array(
                'dateTo' => (new \DateTime())->format('Y-m-d'),
            )))
        ->add('submit', SubmitType::class, array('label' => 'Send', 'attr' => [
            'class' => 'btn-link form-btn'
        ]))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $transactions = $this->get('app')->getTransaction();
    }

    return $this->render('default/finance.html.twig',
        array('form' => $form->createView(), 't' => $t)
    );
}

and my twig view

                <tbody>
                    <tr>
                        <th>ID</th>
                        <th>User</th>
                        <th>Info</th>
                        <th>Name</th>
                    </tr>
                    {% for a in t %}
                        <tr>
                            <td>{{ a.id }}</td>
                            <td>{{ a.user }}</td>
                            <td>{{ a.info }}</td>
                            <td>{{ a.name }}</td>
                        </tr>
                {% endfor %}
                </tbody>`

Solution

  • By the looks of your code your getTransaction repo function is not receiving the $to and $from DateTime objects it should be receiving. Update that function like so:

    public function getTransaction(DateTime $from, DateTime $to) : Collection
    {
        $result = $this->getTransactionRepository()
            ->createQueryBuilder('a')
            ->select('a')
            ->where('a.date >= :from')
            ->andWhere('a.date <= :to')
            ->setParameter('from', $from->format('Y-m-d H:i:s'))
            ->setParameter('to',   $to->format('Y-m-d H:i:s'))
            ->orderBy('p.id')
            ->getQuery()
            ->getArrayResult();
    
        return $result;
    }
    

    And your call should include the parameters now required, like so:

    $transactions = $this->get('app')->getTransaction($from, $to);
    

    I'm not quite sure what's available with FormBuilder, so guessing the full function call would be:

    $transactions = $this->get('app')->getTransaction(
        $form->get('dateFrom')->getValue(),
        $form->get('dateTo')->getValue(),
    );