将日期结果与Doctrine进行比较 [英] Comparing dates results with Doctrine

查看:61
本文介绍了将日期结果与Doctrine进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用表单生成器编写了一个api调用,它创建了两个日期字段.这个想法是选择两个日期,并从该日期范围内的数据分贝中返回结果.

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.

这是来自服务的api调用

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

我的控制器

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

和我的树枝视图

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

推荐答案

通过代码的外观,您的 getTransaction 回购函数未收到 $ to $ from 应该接收的DateTime对象.像这样更新该功能:

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

我不太确定 FormBuilder 可以使用什么,因此猜测完整的函数调用是:

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

这篇关于将日期结果与Doctrine进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆