Destramic Posted August 14, 2011 Share Posted August 14, 2011 below is my model and how it looks...im wondering if that is how it should be done exactly?...i mean is it right that i should have to type my config details in everytime?...should i just get these details passed from a .config file containg the db name, host, username and password? if i could have some advise on this please thank you <?php class League_Model { public function fetch_all() { $config = array('host' => 'localhost', 'username' => 'root', 'password' => 'test', 'database' => 'test'); $db = DB::factory('MySQL', $config); $db->connect(); $variables = array('rank' => '0', 'points' => '0', 'drawing' => '0'); $query = "SELECT *, @rank := IF(@points = points, IF(@points = '0', @rank + 1, @rank), @rank + 1), @points := points, @drawing := IF(@points = points, IF(@points = '0', @drawing = '0', @drawing = '1'), @drawing = '0'), IF(@drawing = '1', @rank + 1, @rank) as rank, @drawing := '0' FROM( SELECT t.team_id, t.team_name, COUNT(r.league_match_result_id) AS 'matches_played', SUM(IF(r.result='Win', 1, 0)) AS `wins`, SUM(IF(r.result='Loss', 1, 0)) AS `losses`, SUM(IF(r.result='Draw', 1, 0)) AS `draws`, SUM(IF(r.result='Win', 3, IF(r.result='Draw', 1, IF(r.result='Loss', 0, 0)))) AS `points` FROM teams t LEFT JOIN league_match_results r ON r.team_id = t.team_id LEFT JOIN team_leagues tl ON tl.team_id = t.team_id WHERE tl.league_id = :1 GROUP BY t.team_id ORDER BY points DESC, t.team_name) AS x"; $league = $db->set($variables)->fetch_all($query, '1'); $rows = $league->get_rows(); return $rows; } public function fetch_row() { } } Quote Link to comment https://forums.phpfreaks.com/topic/244790-model-help-advise/ Share on other sites More sharing options...
ignace Posted August 15, 2011 Share Posted August 15, 2011 A model class does not explicitly have to be named a model to be a model. You don't call your framework classes infrastructure either, right? You can use a Registry to share the config. class League { private $_db = null; public function __construct() { $cfg = Registry::get('DbConfig'); $this->_db = DB::factory($cfg->adapter, $cfg->params); } public function fetchAll() { /*your code here*/ } } Quote Link to comment https://forums.phpfreaks.com/topic/244790-model-help-advise/#findComment-1257602 Share on other sites More sharing options...
Destramic Posted August 16, 2011 Author Share Posted August 16, 2011 interesting...but would it be wise to have a model class which is extended by league_model class which does the mysql connection? Quote Link to comment https://forums.phpfreaks.com/topic/244790-model-help-advise/#findComment-1257930 Share on other sites More sharing options...
Destramic Posted August 20, 2011 Author Share Posted August 20, 2011 does the registry class just read a text file with config options? Quote Link to comment https://forums.phpfreaks.com/topic/244790-model-help-advise/#findComment-1259924 Share on other sites More sharing options...
ignace Posted August 20, 2011 Share Posted August 20, 2011 No. Then would it be a Config class. Quote Link to comment https://forums.phpfreaks.com/topic/244790-model-help-advise/#findComment-1259933 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.