Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Using Criteria::fromInput for date range results

Hi all, I have a simple model called Referral based on this fields: id, referralCode, orderId, createdAt and a simple search controller action. I've added to the form two input in order to select a date range, so the form contains: id, referralCode, orderId, startdate, enddate. Can I manage a find search using Criteria::fromInput to obtain results with createdAt < enddate and createdAt > startdate?


Hi @Cosimo as you can see in Criteria source code only make very simple queries like stringField like '%:stringFieldValue:%' and otherTypesField = ':otherTypesFieldValue:'

In your case you have to do something like this

$query = Referral::query();

if ($startDate) {
$query->andWhere('cretaedAt < {startdate:str}', ['startdate' => $startDate]); // :int if is an timestamp

if ($endDate) {
$query->andWhere('createdAt > {enddate:str}', ['enddate' => $endDate]);

$result = $query->execuate();

Good luck


Thank you Emilio for your reply, as usual. Yes it is what I have done, I was only wondering about another way to do it, but I think i should keep doing this way. Keeping things simple is a good choice because avoid strange behaviours, but maybe comparisons are still simple things and could be something to think about for the framework. I'm asking this because I have very similar views based on these common ways of filtering and I'd like to simplify everything where it's possible