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


12.8k
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;
}


1.6k

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.phalconphp.com/en/latest/reference/models.html.

You can try binding parameter if you like.