Fixing sql on custom controller