In my opinion the calculations seem to be part of the domain model so the DAO is the best place for them.
(pseudo code)
PHP Code:
public function listOfTeams() {
$select = $this->select()
->from(array('t'=>'teams'), array('cod_team','name','score'));
$rows = $this->fetchAll($select);
foreach($rows as &$row) {
$row['percentage_of_games_done'] = $row['totalGamesNumber'] > 0 ? ($row['gamesDone']*100)/$row['totalGamesNumber'] : 0);
}
return $rows;
}
There isn't anything wrong with adding the calculations after the data has been retrieved from the db. If anything this is one of major advantages of separating business logic from application logic. The model data may be derived from any data source, including calculations applied after the data has been fetched from the db.
Alternatively though you could add a flag to mixin the data or place in a separate method. Though, I would only recommend doing this if the process to derive the data is resource intensive. Otherwise, using a consistent interface will avoid problems down the line.
(pseudo code) - separate model methods
PHP Code:
public function listOfTeams() {
$select = $this->select()
->from(array('t'=>'teams'), array('cod_team','name','score'));
return $this->fetchAll($select);
}
public function listOfTeamWithExtraData() {
$rows = $this->listOfTeams();
foreach($rows as &$row) {
$row['percentage_of_games_done'] = $row['totalGamesNumber'] > 0 ? ($row['gamesDone']*100)/$row['totalGamesNumber'] : 0);
}
return $rows;
}
(pseudo code) - flag
PHP Code:
public function listOfTeams($mixin=false) {
$select = $this->select()
->from(array('t'=>'teams'), array('cod_team','name','score'));
$rows = $this->fetchAll($select);
if($mixin === true) {
foreach($rows as &$row) {
$row['percentage_of_games_done'] = $row['totalGamesNumber'] > 0 ? ($row['gamesDone']*100)/$row['totalGamesNumber'] : 0);
}
}
return $rows;
}
Disclaimer: I'm not familiar with how CI returns rows, so the actual implementation may change slightly, though the concept will be consistent.
Bookmarks