简体   繁体   English

将日期结果与教义进行比较

[英]Comparing dates results with Doctrine

I wrote an api call with form builder which creates two date fields. 我用表单生成器编写了一个api调用,它创建了两个日期字段。 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 这是来自服务的api调用

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

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 . 无论如何,我不确定,但是由于您使用的是SELECT AS ,因此我会尝试在查询生成器p.date所有p.date替换为Date Also DATE is reserved keyword in MySQL and you should replace it with something else. 另外, DATE是MySQL中的保留关键字,您应该用其他内容替换它。

By the looks of your code your getTransaction repo function is not receiving the $to and $from DateTime objects it should be receiving. 从代码的外观看,您的getTransaction回购函数未收到应接收的$to$from DateTime对象。 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: 我不太确定FormBuilder什么可用,因此猜测完整的函数调用是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM