开发者

Zend Helper design usage example

I have a view that loops over a database table returning dogs information.

Here's the controller:

public function listAction()
{
  $dogDao = new DogDao();
  $this->view->infoDog = $dogDao->retrieveDogInfo();
}

On the view I can retrieve dog information like this:

<?php for($i = 0; $i < 30; $i++){ ?>
if(isset($this->infoDog[$i])){
   $dog = $this->infoDog[$i]; 

This works fine.

I now need to add additional dog specific information but that dog information, is not an information th开发者_JAVA百科at I can get from the Dog General Info table, it requires some joins, and the retrieval of a number at the end.

public function retrieveVerySpecificInformation($dogId) {

return $numberSpecificForThatDog

};

I (believe) I need a helper that does those calculations and my question is:

I should call this helper on the view where the for resides yes? But how can I connect those informations in a way that I get for given dog A, both: The general dog information AND that numberSpecific for that particular dog A ?

I mean, how can we properly integrate the helper here?

Can I have some help please?

Thanks in advance, MEM


You may call view helper should like this

<?php echo $this->displayDog('A', 'black'); ?>

And your view helper should be like

class Zend_View_Helper_Dog
{
    public function displayDog($type, $color)
    {
         $dog = new Model_DbTable_Dog();
         return $dog->getDog($type, $color);
    }
}

It's OK call view helpers from a loop for couple of DB operations, BUT as you are making custom db query for each dog (30 queries are NOT RECOMMENDED)

Recommendation

You should fetch all info your need with in your controller and pass it to your views, which later can be costumed using any view helper.

public function listAction()
{
    this->view->infoAllDog = $dogDao->retrieveAllDogs(); 
    // including joined info + all dogs you may need in your view
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜