I want to find rows by filtering on a table's column, OR on an associated table's column. I can get an AND version working, but I'm not sure how to change the condition between the ->where()
and the ->matching()
to OR.
In this example I want to find a user by either their username or their regular name. In this case a User hasMany
Names.
$users = $this->Users->find()
->where(['username LIKE' => '%' . $search_term . '%'])
->matching('Names', function($q) use ($search_term){
return $q->where(function($exp, $query) use ($search_term) {
$full_name = $query->func()->concat([
'first' => 'identifier', ' ',
'last' => 'identifier'
]);
return $exp->like($full_name, '%' . $search_term . '%');
});
})
->contain(['Names'])
->limit(10)
->all();