我必须列出几个来自数据库源 A 的元素,它们是: team_id、team_name 和 team_score(为便于解释而翻译)。
我需要遍历它们并显示该信息。
所以,在 DAO 方面,我有:
public function listOfTeams()
{
$select = $this->select()
->from(array('t'=>'teams'), array('cod_team','name','score'));
return $this->fetchAll($select);
}
在我的团队 Controller 上:
public function listAction()
{
$teamsDao = new TeamsDao();
$this->view->infoTeam = $teamsDao->listOfTeams();
}
在 View 中:
<?php for($i = 0; $i < 30; $i++): ?>
<?php if(isset($this->infoTeam[$i])): ?>
现在,问题是,对于列出的每一项,我都需要添加更多信息。
该信息并非直接来自数据库,而是一些计算的结果。
以完成游戏的百分比为例。 (翻译);
$totalGamesNumber > 0 ? ($gamesDone*100)/$totalGamesNumber : 0);
所以,我需要从数据库中获取总游戏数,然后,对于每个团队,我需要完成的游戏数,以便我可以显示百分比。
$gamesDone 通过以下方式获得:
$gameTeamDao->countGamesPerTeam($gameVo, $teamVo);
我被困在这里,因为我看不到我应该在哪里/如何调用和制定计算百分比的方法,以便允许与其他数据一起显示已完成游戏的百分比。
我能从这些泥浆中得到一些帮助吗?
如果您必须为此编写一个助手,或多或少,它会是什么样子?
提前致谢, 内存
PS - 如果您需要更详细的信息。我可以提供。我可能会忘记一些对我来说已经被拿走的东西,但是对于那些想要提供帮助的人来说却不是。所以,请告诉我。再次感谢。
更新:为了帮助,将所有帖子翻译成英文。
最佳答案
由于您使用的是 ZF,因此您可以在表行的类中进行所有这些计算。就是这样。假设您的团队类名为 Application_Model_DbTable_Teams。
class Application_Model_DbTable_Teams extends Zend_Db_Table_Abstract
{
protected $_name = 'teams'; // table name
protected $_id = 'teamId'; // table primary key
// rows returned by fetchAll() will be of this class
protected $_rowClass = 'Application_Model_DbRow_Teams';
}
然后您需要创建 Application_Model_DbRow_Teams 类,您将在其中放置额外的计算
class Application_Model_DbRow_Teams extends Zend_Db_Table_Row_Abstract
{
public $percentGamesDone;
public function init()
{
// This method gets called automatically by ZF when you instantiate a new
// object from this class
// This is where you will put your extra calculations,
// like percentage games done, etc
$this->percentGamesDone = $this->getPercentGames();
}
public function getPercentGames()
{
$calculation = 3; // replace this with real data
return $calculation;
}
}
完成此操作后,您可以直接从 View 中直接使用行的任何已声明属性(例如 percentGameDone),因为它们将在您实例化行时立即计算。
关于php 设计问题 - Helper 会在这里提供帮助吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4298220/