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

Model query by date

Hi everyone,

I have an issue related to query an object using a date as criteria I used ta test to check my problem :

Model.


<?php

class TestDate extends \Phalcon\Mvc\Model
{

    protected $tDate;

    public function getTDate(){
        return $this->tDate;
    }   
    public function setTDate($date){
        $this->tDate = $date;
    }

    public function getSource()
    {
        return "testDate";
    }   

}

and a test method in the controller


public function testDateAction(){

        $theDate = new \DateTime("2015-01-25");
        $myDate = \TestDate::findFirst(array('tDate'=> $theDate->format('Y-m-d')    ));

        var_dump($myDate->getTDate()); die; // Returns null
    }

The database has the following entry

id tdate
0 2015-01-25


21.5k
Accepted
answer

Try

public function testDateAction(){
    $theDate = new \DateTime("2015-01-25");
    $myDate = \TestDate::findFirst(array("tDate = '" . $theDate->format('Y-m-d') ."'"));
    var_dump($myDate->getTDate()); die;
}


3.4k

Thank you very much !!! Two encapsulating quotes costed me so much time ! :'( Is this normal behaviour ? Should not the ORM take care of this ?

I'm afraid that array parameters is reserved for another options in Phalcon's model. Take a look https://docs.phalcon.io/en/latest/reference/models.html.

You can try binding parameter if you like.