I am working on CakePHP 3.4
I am building some application which uses API key. One key is limited to 100 calls per day, so I'm using multiple keys and want to loop through it.
In my controller, I'm calling a model's function to get API
$api_key = $this->CustomApi->getKey();
if (!$api_key) {
$this->Flash->error(__('API key not found'));
} else {
...
}
In model CustomApi
I want to write getKey()
function which will find Api key whose call count of the day is less than 100.
Columns of custom_api
table are id
, api_key
and calls are recorded in api_calls
table whose columns are id
, user_id
, custom_api_id
, created
Everytime user access function which requires API, a record is created in api_calls
table with the time call has been made and primary key of the custom_api.
My question is, How to get Api key from CustomApi
model whose call counts are less than 100 for that day (ie., today)
Edit 2 : My Try
$key = $this
->find()
->select(['CustomApi.id', 'CustomApi.api_key'])
->where(['CustomApi' => /* count is less than 100 */])
->leftJoinWith('ApiCalls', function ($q) {
return $q->where(['ApiCalls.created' => date('Y-m-d')]);
})
->group('CustomApi.id');
How to make a count in where
?