We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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?



32.3k
Accepted
answer

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



12.1k

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