Use models instead of $this->db

Takes care of issue #2225
and use models when appropriet