We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

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 http://docs.phalcon.io/en/latest/reference/models.html.

You can try binding parameter if you like.