1

I want use right MVC concept. So, say in model we have for example query to DB

    class my_model {

        public function getUserData () {
            $res = $this->db->query("
                SELECT name, lastname, age, mail FROM users
            ");

            return $res;
        }

    }

What I do: I pass mysqli_result object to view and after in view in while cycle I print data from DB.

But as I know, pass mysqli_result to view, isn't right MVC.

So, can you show me, what is right MVC solution, in situations like this ?

tereško
  • 58,060
  • 25
  • 98
  • 150
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236
  • 1
    As in Zend Framework. You call your Create a controller, call your model object, then you create the View Object and pass on the returned Model Object to the View object. – Joddy Dec 14 '12 at 09:00
  • If you were actually implementing something that is even remotely related to MVC, your view instance (as in "made from object of a class") would request data from model layer (as in "not any particular class but representing an amalgamation of different types of classes"). What you currently have is not even vaguely similar to MVC. – tereško Dec 15 '12 at 08:25

3 Answers3

1

In MVC, the view would request the data from the model directly and would not be passed data by anything. The view has a dependency on the model and requests its own data. See this answer: How is MVC supposed to work in CodeIgniter for a more detailed description on why this is so.

The answer is to initialise the view after the model and pass the view the entire model in the constructor.

e.g.

class View {
    private $model;

    public function __construct(my_model $model) {
        $this->model = $model;
    }

    public function output() {
        $html = '<ul>';
        foreach ($this->model->getUserData() as $row) {
            $html .= '<li>' . $row->name . '</li>';
        }
        $html .= '</ul>';
        return $html;       
    }
}
Community
  • 1
  • 1
Tom B
  • 2,735
  • 2
  • 24
  • 30
0

I think you should implement a DTO class which holds the data of your users. Then you should use this DTO class with an Iterator, so that you don't initialize 100's of DTO classes when fetching the result. A Good practice is also to implement a service facade which does the actual DB query and iterator building, so that your model is only calling this repository.

Jan.
  • 1,925
  • 15
  • 17
-1

In my MVC, I would do something similar to this:

class my_model {
  public function getUserData () {
    $res = $this->db->query("
      SELECT name, lastname, age, mail FROM users
    ");

    while ($row = mysql_fetch_assoc($res)) {
      $results[] = $row;
    }
    return $results;
  }
}

class my_controller {
  function index() {
    $user_data = $my_model->getUserData();
    include("view.php");
  }
}



  // view.php
  echo "<ul>";
  foreach ($user_data as $d) {
      echo "<li>" . $d[keyname] . "</li>";
  }
  echo "</ul>";
Rob
  • 1,840
  • 2
  • 12
  • 19