Welcome Guest, Not a member yet? Register   Sign In
What's the right place for sort algorithm? Model? View? Controller?
#1

[eluser]Unknown[/eluser]
Hi!

I am new in MVC frameworks, so I have a question related to MVC paradigm.

I have SQL table with number of points with GPS data (lat/long).
And I have php algorithm (function) that can sort points by distance from any point.

My question is what is the correct place for that sort function?

1. May be model function should take parameters like $sort_by_point_lat $sort_by_point_long and sort these points
Code:
function get_points($sort_by_point_lat, $sort_by_point_long) {}

2. Or may by controller should sort them as it received from model
Code:
function show_points()
{
$my_points = $this->Points->get_points();
$this->sort_by_distance($my_points,123,456);
}

3. Or may be View should do it then it displays these points.


What do you think? Thanks!
#2

[eluser]davidbehler[/eluser]
I would go with the first option and put it in the model file.
View and controller are definetly the wrong places as the view only displays the data it gets from the controller and the controller does all the logic between getting the data (from the model) and displaying it (in the view file).
#3

[eluser]basementDUDE[/eluser]
[quote author="waldmeister" date="1250097415"]I would go with the first option and put it in the model file.
View and controller are definetly the wrong places as the view only displays the data it gets from the controller and the controller does all the logic between getting the data (from the model) and displaying it (in the view file).[/quote]

I believe model should only deal with database data, so get data sort out in the model is a good idea.
#4

[eluser]renownedmedia[/eluser]
What if you want to use the same model in different controllers, and different controllers sort the data differently?
#5

[eluser]rogierb[/eluser]
Sorting should be done in the model. There are two different approaches I use when dealing with sorting.
1: parse the order_by as a variable to you model
2: create different order_by's in you model and use one of them depending on a var you parse to the model.

I prever the last approach.
#6

[eluser]Unknown[/eluser]
Thanks you all for answers! It very helpful.
#7

[eluser]wabu[/eluser]
And don't forget if something's more complex you always have the option of isolating it in its own module/library/helper thingy.

But in this case it sounds relatively simple so my vote is for the model. Wink
#8

[eluser]adamp1[/eluser]
I would say the actual sort code should go in a library. Then the model should use the library to be able to return the sorted data.




Theme © iAndrew 2016 - Forum software by © MyBB