• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MVC Structure Question

#1
[eluser]drshields[/eluser]
This is a general question relating to MVC development.

I want to add a value to a database if the value doesn't currently exist, and then return the insert id of that entry. If the value already exists, just return the id of that entry.

Do I do the logic piece of that in the controller, or in the model? Doing the controller requires two functions, where doing it in the model requires only one.

EXAMPLE

From the controller, I call one function to see if it exists, and another to add it if it doesn't exist:

Code:
if ($this->database_model->value_exists($some_value) == NULL)
{
   $new_id = $this->database_model->add_value($something);
}

Now if I put the logic in the model, my controller looks like this:

Code:
$value_id = get_value($something);

In my model:

Code:
function get_value($something)
{
  // do query to get value.  if it exists, return the id
  // if value doesn't exist, insert $something and return id
}

What is the "proper" way to do this? Does it matter?

#2
[eluser]Michael Wales[/eluser]
I'd put it in the model as it is a way of accessing the data the model represents.

The controller would be used to implement any class specific processes you want to apply to that data (if it's app specific - throw it in the model) and to pass that data on to the view.

View would do any final processing to make that data presentable (applying CSS classes, using PHPs date() function to turn 2008-06-09 into "Mon, 9 Jun", etc.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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