Moving database behaviour into models