You can specify mysql functions in the 'from' function, which executes the select query function. To use the from function, you need to pass the table name as the first parameter, however passing $ this (your table model class) works fine.
public function countFollowers($user_id) { $rowset = $this->fetchAll( $this->select() ->from($this, array('DISTINCT user_id')) ->where('user_id = ?', $user_id) ); return count($rowset); }
[edit]
Based on your editing, a “group” may also work for you:
public function countFollowers($user_id) { $rowset = $this->fetchAll( $this->select() ->where('user_id = ?', $user_id) ->group('user_id') ); return count($rowset); }
This will group all matching user_id into one record. Therefore, if the user is found, it will return 1, otherwise 0.
Richard Parnaby-King
source share