I have following schema:
User hasMany RegistrationState RegistrationState belongsTo User
to its classic 1->M relation where user_id is in RegistrationState table.
RegistrationState has following attributes
id, user_id, state, created_at ... ( others are not relevant )
User can have more registration states for example:
Registration state -> 1, created -> 01-02-2017
Registration state -> 2, created -> 01-03-2017
Registration state -> 3, created -> 01-04-2017
Registration state -> 4, created -> 01-05-2017
Registration state -> 2, created -> 01-06-2017
Registration state -> 3, created -> 01-07-2017
I need to filter based on this 'state property', where the valid state is last one created.
Valid state for this example is 3, because its latest state.
Now, i have controler, and index method where i have
// push custom query for filtering to finder method
$this->paginate['finder'] = [
'clients' => ['q' => $this->request->query]
];
try {
$clients = $this->paginate( $this->Users );
} catch (\Exception $e) {
// redirect on main
return $this->redirect($this->here);
}
My finder method looks like this
public function findClients($query, array $options)
{
$opts = $query->getOptions()['q'];
if (!empty($opts['email'])) {
$query->andWhere(['email LIKE' => '%' . trim($opts['email']) . '%']);
}
if (!empty($opts['identity_num'])) {
$cn = $opts['identity_num'];
$query->matching('Identities', function ($query) use ($cn) {
$query->andWhere(['Identities.number' => $cn]);
});
}
// registration state
if (!empty($opts['reg_state'])) {
// WHAT SHOULD BE HERE???
}
return $query;
}
There is also another 1->M relation User -> Identity, but ( matching method ) but that works fine because number is always unique.
I can't resolve problem with registration states, how can I implement this kind of search in CakePHP3? ( i dont want to break pagination so i would like to solve this in finder method )