Comparing dates results with Doctrine

懵懂的女人 提交于 2019-12-11 17:42:54

问题


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>`

回答1:


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(),
);



回答2:


First of all, you should specify what error you get.

Anyway, I'm not sure but I would try to replace all p.date with Date in your query builder, since you are using SELECT AS. Also DATE is reserved keyword in MySQL and you should replace it with something else.



来源:https://stackoverflow.com/questions/54291954/comparing-dates-results-with-doctrine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!