开发者

Assigning Object to View, big MySQL resultset

开发者 https://www.devze.com 2022-12-27 13:03 出处:网络
Hello (Sorry for my bad English) Is it bad practice to assign object to view and call its methods in there?

Hello (Sorry for my bad English)

Is it bad practice to assign object to view and call its methods in there? I use smarty as my template engine.

In my controller I could do like this

1#

$this->view->assign("name", $this->model->getName);

and in my view

<p>{$name}</p>

OR

2#

$this->view->assign("Object", $this->model);

and in my view

<p>{$Report->getName()}</p>

Well my biggest problem is that I have to handle a big amount of data coming out from the MySQL and I thought that if I would made a method that would print out the data while looping mysql_fetch_row. Well at least I know that using html-tags in the model is a bad thing to do.

So I would assign the object to the view to get the result to the right position on the page..

Reading a mysql-result to an array first may cause me开发者_开发技巧mory problems am I right? So what is the solution doing things MVC style..

And yes Im using a framework of my own.


Ideally you should use the first approach. Unfortunately we don't live in an ideal world. If it's too much of a pain to do that I don't really see a problem with the second approach. Isn't there a way to compromise though? If you could get the names of the columns from your model as an array, you could loop through them and assign each value to smarty dynamically. That way your view would be simpler and you'd avoid a lot of code in the controller.

As for taking too much memory... it seems improbable that the amount of data you'd post in one page would take up too much memory. I think you'd be having UI problems (too much stuff in a single page) long before you have memory problems. In any case why don't you try a simple benchmark. You could use PHP's memory_get_usage() function for that.


In terms of MVC, I have seen the mostly the first method being used. Put all your processing logic in the controller and use the View only for display, no need to put processing ({$Report->getName()}) there in the view.


from my point of view there's nothing wrong about Views talking directly to the Model. Moreover, that is how the classic "triangular" MVC is supposed to work.

0

精彩评论

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

关注公众号