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

Find all Between two dates with ::find()

Does Phalcon's ODM support between finds?

I am trying to get a list of objects based on their dates. So i need to be able to look for dates between X and Y.

I could Do > and < or i could use Betwen. I don't really care which one, but i can't seem to get either to work.

  $beginDate    = "2015-01-01 00:00:00";
  $endDate      = "2015-02-01 00:00:00";
  Model::find(array(
    'conditions' => array(
          'userId' => $user->id,
          'signDate >' => $beginDate,
          'signDate <' => $endDate
      )
  ));

Now obviously this is not using the sql Between function. I would much prefer something liek this.

  $beginDate    = "2015-01-01 00:00:00";
  $endDate      = "2015-02-01 00:00:00";
  Model::find(array(
      'conditions' => array(
          'userId' => $user->id,
          'signDate >' => array(
                'between' => array(
                    $beginDate,
                    $endDate
                )
          )
      )
  ));

This is what i am looking for on the sql select side.

SELECT * FROM model WHERE userId = '{$user->id}' AND signDate BETWEEN '{$beginDate}' AND '{$endDate}'


18.6k
Accepted
answer

I found the answer. YAY for reading the documentaiton.

You have to bind your data to a single condiiton statment.

$test = Accounts::find(array(
    'conditions' => 'userId = ?1 AND signDate BETWEEN ?2 AND ?3',
    'bind' => array(
        1 => $user->id,
        2 => $dates[0],
        3 => $dates[1])  
));