view中使用了逻辑,如何取出来

发布于 2024-10-09 18:57:35 字数 247 浏览 0 评论 0原文

对于一个项目,我在我看来使用了一些逻辑,这不是正确的方法,所以我想把它拿出来。

问题是它无法从我的模型的类方法中完成,因为 Zend 将从 10000 个实例到数据库进行 10000 次查询,并且它变得非常慢。

因此,我必须一次性加载所有数据,然后对其进行处理并将数据返回到视图。在我看来,它的工作方式与我的方式相同,唯一的问题是它位于视图文件中。

该怎么走呢?只需在模型中创建一个类来输入值并返回所需的数据?

谢谢

For a project I used some logic in my view, this is not the way to go so I want to get it out.

The problem is that it can't be done from a class method of my model because Zend will make 10000 of queries from 10000 of instances to the database and it becomes very slow.

So I have to do it on a way that it loads all data at once, then processes it and returns the data back to the view. In my view it works the way I do it, the only problem is that it is IN the viewfiles.

What is the way to go? Just make a class in the model that inputs the values and returns required data?

Thanks

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

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

发布评论

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

评论(1

不甘平庸 2024-10-16 18:57:35

这是我从 MVC 角度显示数据的方式

Controller

function someAction(){
    $someTable = new Model_DbTable_SomeTable();
    $allData = $someTable->fetchAll();

    $arrayFormattedData = DataProcessor::process($allData);

    $this->view->data = $arrayFormattedData;
}

你必须在模型中进行逻辑处理(在上面的示例中,它通过 process 方法在静态类 DataProcessor 中完成(不是必然是要走的路,但这可能是一个好的开始)

查看

echo $this->dataParser($this->data); // using a view helper to parse data to be displayed

echo $this->partialLoop('partialLoop.phtml', $this->data); // using the partial loop view helper built in in ZF

最后,您应该尝试使模型尽可能灵活,使其可重用,这就是oop 开发的关键。

Here is the way i would go to display data from a MVC perspective

Controller

function someAction(){
    $someTable = new Model_DbTable_SomeTable();
    $allData = $someTable->fetchAll();

    $arrayFormattedData = DataProcessor::process($allData);

    $this->view->data = $arrayFormattedData;
}

You have to do your logic processing in a model (in the example above its done in the static class DataProcessor throught the process method (Not neccessarly the way to go, but it could be a good start)

View

echo $this->dataParser($this->data); // using a view helper to parse data to be displayed

or

echo $this->partialLoop('partialLoop.phtml', $this->data); // using the partial loop view helper built in in ZF

Finally, you should try to make your models as flexible as possible to make them reusable which is the key in oop development.

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