B
B
BonBon Slick2018-09-04 10:29:19
SQL
BonBon Slick, 2018-09-04 10:29:19

Difference of requests at selection with dates?

This query works as it should, makes a selection in the
Doctrine ORM range

$query->andWhere('orders.createdAt BETWEEN :startOrderCreateDate AND :endOrderCreateDate')->setParameter(
                'startOrderCreateDate',
                $dateFrom
            )->setParameter(
                'endOrderCreateDate',
                $dateTo
            );
        }

And now separate 2 requests, for example, if the user has selected a date FROM, or only the end TO
//от
            $query->andWhere('orders.createdAt >= :startOrderCreateDate')->setParameter(
                'startOrderCreateDate',
                $dateFrom
            );

// до
             $query->andWhere("orders.createdAt <= :endOrderCreateDate")->setParameter(
                'endOrderCreateDate',
                $dateTo
            );

The second option works if the date is 1, for example, select everything where the date is less than the current one, but only if there are two dates (as in the sample above, they exist, of the required format, etc.), one of andWhere stops working, and selects one filter , why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexalexes, 2018-09-04
@alexalexes

The simplest solution, but you need to know the state when the parameter is not set in the form:
if it does not come at all, then use the isset () check.
if somehow equates to null, then ! is_null();
if it has a default value, then either on. this condition, or cut off $dateFrom != $default_dateFrom.

if(isset($dateFrom))
{
        $query->andWhere('orders.createdAt >= :startOrderCreateDate')->setParameter(
                'startOrderCreateDate',
                $dateFrom
            );
}
if(isset($dateTo))
{
   $query->andWhere("orders.createdAt <= :endOrderCreateDate")->setParameter(
                'endOrderCreateDate',
                $dateTo
            );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question