php设计问题 - 帮助帮助吗? [英] php design question - will a Helper help here?

查看:159
本文介绍了php设计问题 - 帮助帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须列出来自数据库源A的几个元素,它们是:
team_id,team_name和team_score(为了解释起见)。



我需要循环,并显示这些信息。



所以,我在DAO方面:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$''''''''''''''''''''''''''''''''''''' t'=>'team'),array('cod_team','name','score'));
return $ this-> fetchAll($ select);

}

在我的团队控制员:

  public function listAction()
{
$ teamsDao = new TeamsDao();
$ this-> view-> infoTeam = $ teamsDao-> listOfTeams();
}

在视图中:


$ b $ ($ i = 0; $ i< 30; $ i ++):?>

<?php if(isset($ this-> infoTeam [$ i])):?>

现在,在每个列出的项目上,我需要添加更多信息。



该信息不直接来自数据库,但这是一些计算的结果。



例如游戏的百分比。 (翻译);

  $ totalGamesNumber> 0? ($ gamesDone * 100)/ $ totalGamesNumber:0); 

所以,我需要从数据库中获取总游戏编号,然后,对于每个团队,我需要有完成的游戏数量,以便我可以显示百分比。



$ gamesDone是通过以下方式获得的:

  $ gameTeamDao-> countGamesPerTeam($ gameVo,$ teamVo); 

我被困在这里,因为我看不到哪里/应该怎么打电话计算百分比的方法,以使游戏完成的百分比与其他数据一起显示。



我可以一些帮助从这个泥土?



如果你不得不为此写一个帮手,或多或少,它会是什么样子?

提前感谢
MEM



PS - 如果您需要更多详细信息。我可以提供我可以忘记一些对我来说有用的东西,但是对于那些想帮助不了的人,所以,只是让我知道。再次感谢。



更新:将所有帖子翻译成英文,以求帮助。

解决方案

由于您正在使用ZF,您可以在表行的类中进行所有这些计算。就是这样。假设你的团队类名为Application_Model_DbTable_Teams。

  class Application_Model_DbTable_Teams扩展Zend_Db_Table_Abstract 
{
protected $ _name = 团队; // table name
protected $ _id ='teamId'; //表主键

// fetchAll()返回的行将为此类
protected $ _rowClass ='Application_Model_DbRow_Teams';

}

然后,您需要创建Application_Model_DbRow_Teams类,额外的计算

  class Application_Model_DbRow_Teams扩展Zend_Db_Table_Row_Abstract 
{
public $ percentGamesDone;

public function init()
{
//当您从此类实例化一个新的
//对象时,ZF会自动调用此方法
//这是你将额外的计算,
//喜欢百分比游戏完成等等
$ this-> percentGamesDone = $ this-> getPercentGames();
}

public function getPercentGames()
{
$ calculate = 3; //用真实数据替换这个
return $ calculate;
}
}

一旦你这样做,你可以简单地使用直接从视图中的任何一行声明的属性(如percentGameDone),因为它们将在您执行行时计算正确。


I have to list several elements that are coming from a database source A and they are: team_id, team_name and team_score (translated for explanation sake).

I need to loop over them, and display that information.

So, I have, on the DAO side:

public function listOfTeams()
{
  $select = $this->select()
    ->from(array('t'=>'teams'), array('cod_team','name','score'));
  return $this->fetchAll($select);

}

On my team controller:

public function listAction()
{
  $teamsDao = new TeamsDao();
  $this->view->infoTeam = $teamsDao->listOfTeams();                    
}

And at the view:

<?php for($i = 0; $i < 30; $i++): ?>

  <?php if(isset($this->infoTeam[$i])): ?>

Now, the thing is, on each of those listed items, I need to add more information.

That information doesn't come directly from a database, but it's a result of some calculations.

Take for example the percentage of games done. (translated);

$totalGamesNumber > 0 ? ($gamesDone*100)/$totalGamesNumber : 0);

So, I need to grab the total games number from the database, then, for each team, I need to have the number of games done, so that I can have the percentage to be displayed.

The $gamesDone are obtained by:

$gameTeamDao->countGamesPerTeam($gameVo, $teamVo);

I'm stuck here, because I cannot see where/how should I call and make the method for calculating the percentage, in order to allow the percentage of games completed to be presented along with the other data.

Can I have some help out of this mud ?

If you had to write a helper for this, more or less, how will it looked like?

Thanks in advance, MEM

PS - If you need more detailed information. I can provide. I could be forgeting something that for me is taken, but that, for those who want to help it isn't. So, just let me know. Thanks a lot again.

Update: translated all post to English for help sake.

解决方案

Since you are using ZF, you could make all this computations in the table row's class. Here's how. Let's say your teams class is called 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';

}

Then you need to create the Application_Model_DbRow_Teams class where you will put the extra calculations

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;
    }
}

Once you've done this, you can simply use any of the row's declared properties (like percentGameDone) directly from the view as they will be calculated right when you instanciate the rows.

这篇关于php设计问题 - 帮助帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆