Using model functions in View |
[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 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?
[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.
[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.
[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.
[eluser]daparky[/eluser]
That works to a certain extent. What if the client has more than one domain name?
[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 Or backup your files very carefully!
[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.
[eluser]Colin Williams[/eluser]
When doing joins, you always need to iterate through the result and group multiple values.
[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.
[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. http://www.phpwact.org/pattern/model_view_controller |
Welcome Guest, Not a member yet? Register Sign In |