• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using model functions in View

I'm just wondering if it's bad practice to use model functions in a view file, for example, i have the following in a view file:-

            if($count > 0) {
            foreach($clients as $clients) { ?>
                <td>&lt;?= $clients->name; ?&gt;</td>
                <td>&lt;?= $clients->email; ?&gt;</td>
                $getdomains = $this->Extra->getDomains($clients->id);
                $num = count($getdomains);
                if($getdomains > 0)
                    foreach($getdomains as $getdomains){ $i++; ?&gt;
                    &lt;?= $getdomains->dname; ?&gt; &lt;?php if($i == $num) { echo ''; } else { echo ','; } ?&gt;

                &lt;?php }
                } ?&gt;
            &lt;?php }
            } ?&gt;

The reason i put $getdomains = $this->Extra->getDomains($clients->id); in the view is because i couldn't get it working in the controller.

Just wondered what your thoughts of putting model calls in view?

I haven't used model's functions in my views, but as much as i can see from your code, you should considering using LEFT JOIN or INNER JOIN depending on how your database tables are connected, cause that way you can collect all rows you need (including the ones for the domains), fetch them in the controller and pass them to the view instead of doing so many queries. Also try reading something about indexing columns in MySQL for faster querying. I could help you more if you just post the tables from the database, how they are organized. Also the way you are doing would be something if you really really don't have any other solution.

Thanks for your reply Zorancho. I tried a left join, i was querying the client table and joining the domains table in where client.id = domain.client_id. I was having issues doing it this way because of duplicate results.

What was happening was that depending on how many domains the client had, the result of that client kept repeating until the number of domains reached. For example:-

Client 1 has ten domains
The loop repeated Client 1 ten times, each object had the different domain in.

What i wanted to achieve (i achieved this using the above method) was listing all clients, displaying info from the client table and then going into the domain table and getting all the domains the client holds.

What about this kind of SQL statement?
$sql = "SELECT c.name, c.email, DISTINCT(d.dname) FROM client c LEFT JOIN domain d ON c.id = d.client_id";

This way you will select name and email from client and no duplicate results for domain name.

That works to a certain extent. What if the client has more than one domain name?

Daparky, since the goal of MVC is to separate data manipulation from template files, it's always bad practice to call model functions from the view files. Though it is possible to so... So at the end it's still up to you!

If you are working with a designer who doesn't know much about PHP, you might want to avoid putting too much of it inside your views Wink Or backup your files very carefully!

Technically, MVC principles state that if you want to display certain content you should pull the data you want using a Model into your Controller, use the Controller to load the data you want displayed, then pass the data to the View for displaying.

The idea is that the View should be agnostic, or in fact totally ignorant, of where its information/content comes from. All it knows is it should be passed certain variables, and it should display them nicely.

The upside is if at some point you want to load or access the information in a different way you should not need to edit the view. The goal is to eliminate spaghetti code - meaning, code where if you have to change anything you have to go through all sorts of different files to make minor adjustments.

[eluser]Colin Williams[/eluser]
When doing joins, you always need to iterate through the result and group multiple values.

Thanks for all the help people, Codeigniter is the first MVC framework i've used and have to say, it's amazing.

I'll keep this in mind for the future.

@BrianDHall and interested others,

Quote: The view obtains data from the model and presents it to the user. The view represents the output of the application.

The view generally have free access to the model, but should not change the state of the model. Views are read only representations of the state of the model. The view reads data from the model using query methods provided by the model.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.