Custom Finder Method in CakePHP 3

In your Table Model create a method and name it, findMymethod, ‘find’ prefix is mandatory, for ex.

public function findOffline(Query $query, array $options)
        'online'    =>  false,
    return $query;


$customers = TableRegistry::get('Customers');
$query = $customers->find('offline');
$query = $customers->find('offline')->where(['column'=>'value']);
$query = $customers->find('offline')->order(['lastseen' => 'DESC']);
//or you can also ‘stack’ finders, 
//allowing you to express complex queries effortlessly.
$query = $customers->find('offline')->find('expired');
//Assuming you have ‘expired’ finder.

For more, visit CakePHP 3 Book.

Categories: CakePHP, PHP Tags: , , ,
%d bloggers like this: