Jump to content

Recommended Posts

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()
{

}
}

Link to comment
https://forums.phpfreaks.com/topic/244790-model-help-advise/
Share on other sites

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*/
    }
}

Link to comment
https://forums.phpfreaks.com/topic/244790-model-help-advise/#findComment-1257602
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.