artSir
artSir

Reputation: 550

Cakephp 3 find() within 2 hours

This is pretty straight forward but I can't find it in the manual. I did this in cake 2 but can't find how to use the new ORM. I am doing a basic user changed password token. I want to check if the user was modified within the last 2 hours

 $query = $this->Users->find('all', [
        'conditions' => ['token' => $token],
        'limit' => 1,
        'where'=> ['modified' =>  /*new Datetime(-2 hours )*/  ]
    ]);

thank you

Upvotes: 1

Views: 656

Answers (1)

user221931
user221931

Reputation: 1852

There seem to be several issues with the snippet you provide. The -2 hours argument of DateTime isn't inside quotes and modified doesn't have a comparison operator. Furthermore, you're using an array of options for find() which means all WHERE arguments should be inside conditions key not in a separate where key.

This should work:

$query = $this->Users->find('all', [
    'conditions' => [
        'token' => $token,
        'modified >' =>  new \DateTime('-2 hours'),
    ],
    'limit' => 1,
]);

Upvotes: 3

Related Questions