Zend Helper 设计使用示例

发布于 2024-10-07 00:06:47 字数 770 浏览 2 评论 0 原文

我有一个循环遍历返回狗信息的数据库表的视图。

这是控制器:

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

在视图上,我可以像这样检索狗信息:

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

这工作正常。

我现在需要添加额外的狗特定信息,但是该狗信息不是我可以从“狗一般信息”表中获取的信息,它需要一些连接,并在最后检索一个数字。

public function retrieveVerySpecificInformation($dogId) {

return $numberSpecificForThatDog

};

我(相信)我需要一个助手来完成这些计算,我的问题是:

我应该在 for 所在的视图上调用这个助手,是吗? 但是我怎样才能以我为给定的狗 A 获得的方式连接这些信息,两者都是: 狗的一般信息和特定于该特定狗的数字 A ?

我的意思是,我们如何才能正确地将助手集成到这里?

我可以帮忙吗?

提前致谢, MEM

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 that 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有一腔孤勇 2024-10-14 00:06:47

您可以像这样调用视图助手,

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

并且您的视图助手应该像

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

这样OK从循环中调用视图助手进行几个数据库操作,但是当您为每只狗进行自定义数据库查询时(

建议

您应该在控制器中获取您需要的所有信息并将其传递到您的视图,稍后可以使用任何视图助手对其进行定制。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文