MVC: Can a loop view query results?

I am new to MVC. You have been warned ...

I have a User model that can return the MySQL result resource to the controller. The controller passes the MySQL resource to the displayed view. Is it acceptable to print query results in a view using the database results fetch function?

<?php while($row = some_fetching_function($database_result_resource): ?> <tr> <td><?=$row['Username']?></td> ...etc </tr> <?php endwhile; ?> 

This does not look right to me. This is closely related, right? A model must return some type of database resource, and a view must pass through it using a data type method of a certain type. Can it be unleashed without repeating the results twice? I think you will have to iterate over the results in the model to create an array of results, and then again in the view.

In short:

  • Can a view display a database result resource by adhering to the MVC design pattern?
  • Can data cycling be avoided twice by avoiding hard-linking to the database?
+4
source share
4 answers

If you distract part of the code database, I find it acceptable. For example, you can provide a β€œrowset” that can be iterated (implement an Iterable interface or something else). Behind the scenes, this object may contain the database result and use the fetch function.

Basically, the idea is that your view deals with a generic search set of strings that does not imply that it comes from a database or any other source, and thus you reduce the connection.

+6
source

Of course it can. But in fact, you should not get strings in your view. Executing the task of model data in the controller, but not directly in the view. As Huray Im Helping pointed out, data sampling depends on the model you are using. But you should not have any specific database methods or model logic. If the model implements a common Iterator interface, you can pass it directly to the view.

 <?php public function someControllerAction($params) { $myModel = Model::getModel('Model Name'); // But you don't do Model::getModel('Model Name')->getResults($params['date']) in your viewa $this->view->rows = $myModel->getResults($params['date']); } ?> 
+2
source

As Yani said, you should probably transfer the MySQL result to a class that implements the Iterable interface from the standard PHP library. That way, your view can use the familiar and general foreach construct without requiring you to repeat twice in your results to put it in an array.

SPL

  class rswrap implements Iterator
 {
     var $ rs;
     var $ current;

     function __construct ($ rs)
     {
         $ this-> rs = $ rs;
     }

     function rewind ()
     {
         // do nothing
     }

     function next ()
     {
         $ this-> current = some_fetching_function ($ this-> rs);
     }

     function current ()
     {
         return $ this-> current;
     }

     function valid ()
     {
         return $ this-> current! == false;
     }

     function key ()
     {
         return 0;  // usually sufficient but you may want to write a real key function.
     }
 }

 $ resultset = new rswrap ($ database_result_resource);

 foreach ($ resultset as $ row)
 {
     // your code here
 }
0
source

I think the best way is to divert the results of the SQL query into the list of objects and return the list.

I don’t think that repeating a rowset will disconnect the view from the particular database you are using. The view should be independent of the database connection used. If you decide to use JDBC or hibernation, or that the transition does not require changes to the view.

0
source

All Articles