FarhanKhalaf Posted July 25, 2009 Share Posted July 25, 2009 I just recently dove into OOP & now MVC and am using this template engine : http://www.milesj.me/resources/script/template-engine I am curious about one question on where to put my DB calls (I'm using a basic database wrapper class). I've seen two ways done. class Cart /** * Counts items in cart * @return int */ public static function count() { require_once(DATABASE .'cartext.php'); $info = User::getInfo(); $count = CartExt::inCart($info['0']['userid']); return $count; } // Seperate page class CartExt /** * user cart count * @param int * @return int */ public static function inCart($shopperID) { $db = Database::getInstance(); $query = $db->execute("SELECT * FROM Listing WHERE shopperid = '$shopperID'"); $count = 0; while ($row = $db->fetchAll($query)) { $count++; } return $count; } With large functions I can see the advantage of separating the two, but a lot of the time it's as mundane as the example above, or worse: the base class just calls upon the Ext and returns its value! Also, I am doing a require_once from within the function to lower http requests if anyone is asking. Anyway, I just want some thoughts on this. Also, am I correct in that I should handle $_POST['data'] in the controller and pass it as an param to my functions there, opposed to handling it within the class? (I'm not using a form object/class yet if it matters). Looking forward to hearing your thoughts on this. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 25, 2009 Share Posted July 25, 2009 I am doing a require_once from within the function to lower http requests require_once() has nothing to do with http requests. What you actually mean is that you are lazy loading the file. I am curious about one question on where to put my DB calls (I'm using a basic database wrapper class). These belong in the models (and don't use wrapper's, you are only limiting the object you are wrapping). Also, am I correct in that I should handle $_POST['data'] in the controller and pass it as an param to my functions there, opposed to handling it within the class? (I'm not using a form object/class yet if it matters). I am not entirely sure wether I should say yes or no. A front-controller uses a router to route a request (http, ftp, ..) to a controller and it's action. In the action are/is a certain model(s) (wether db or file) called, wrapped (view doesn't need accessor functionality) and passed to the view which handles the proper rendering wether it's html, xml, json or plain text. Everyone always said: skinny controllers, fat models. Nowadays they are even saying: skinny models, fat service layers. A service layer is based on a use-case and can be something like: $userService->registerUser($request); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.