Using ORM instead of RAW in cakephp

Replaces #682 by rebasing those commits onto master, where the existing issues with cakephp have been corrected. 

Have tested locally, will wait until PR is tested by Travis to merge.