CodeIgniter Forums
Using model functions in View - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Using model functions in View (/thread-22783.html)

Pages: 1 2


Using model functions in View - El Forum - 09-20-2009

[eluser]daparky[/eluser]
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:-

Code:
<?php
            if($count > 0) {
            foreach($clients as $clients) { ?>
            <tr>
                
                <td>&lt;?= $clients->name; ?&gt;</td>
                <td>&lt;?= $clients->email; ?&gt;</td>
                <td>
                &lt;?php
                $getdomains = $this->Extra->getDomains($clients->id);
                $i=0;
                $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;
                </td>
            </tr>
            &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?


Using model functions in View - El Forum - 09-20-2009

[eluser]Zorancho[/eluser]
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.


Using model functions in View - El Forum - 09-20-2009

[eluser]daparky[/eluser]
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.


Using model functions in View - El Forum - 09-20-2009

[eluser]Zorancho[/eluser]
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.


Using model functions in View - El Forum - 09-20-2009

[eluser]daparky[/eluser]
That works to a certain extent. What if the client has more than one domain name?


Using model functions in View - El Forum - 09-20-2009

[eluser]benoa[/eluser]
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!


Using model functions in View - El Forum - 09-20-2009

[eluser]BrianDHall[/eluser]
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.


Using model functions in View - El Forum - 09-20-2009

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


Using model functions in View - El Forum - 09-20-2009

[eluser]daparky[/eluser]
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.


Using model functions in View - El Forum - 09-20-2009

[eluser]wiredesignz[/eluser]
@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.

http://www.phpwact.org/pattern/model_view_controller