Reputation: 1601
I have a function in my model that need information from another function (generate) in the model. Should I call it from the controller or from the model?
Call generate from the controller
class Inventory extends Controller {
function add_stock()
{
$generate = $this->model->generate(); //call generate from controller
$this->model->current_stock($generate);
}
}
class Model {
public function current_stock($generate)
{
//do something with information from generate
}
public function generate()
{
//do something
return
}
}
OR
In the model
class Inventory extends Controller {
function add_stock()
{
$this->model->current_stock();
}
}
class Model {
public function current_stock()
{
$generate = $this->generate(); //call generate from model
//do something with information from generate
}
public function generate()
{
//do something
return
}
}
Both works but which is the best solution?
NOTE! This is just code exemel and can contain errors, just want to illustrate what I mean.
Upvotes: 0
Views: 37
Reputation: 1756
Just minor change, and now you can use it by calling $this->model->add_stock();
in your controller. Additionally, you can write tests for each method without any problem ;)
Benefits:
$this->model->add_stock();
method without worrying about any errors.$this->model->add_stock();
in Contoller, I know what this method do. I don't need even to know about generator or current stock.class Inventory extends Controller
{
function add_stock()
{
$this->model->add_stock();
}
}
class Model
{
public function current_stock($generate)
{
//do something with information from generate
}
public function generate()
{
//do something
return;
}
function add_stock()
{
$generate = $this->generate();
$this->current_stock($generate);
}
}
Upvotes: 0
Reputation: 1701
In this use-case, I would definitely say the latter. It appears that generate() operates exclusively through the Model class and ought to be a private method (not a protected or public method).
In essence, the first example is accessing a (should be) private method and duplicating that model's logic.
$generate = $this->model->generate();
The above is redundant code; this will become more and more apparent the more validation you perform.
Upvotes: 0
Reputation: 10217
The question is whether this is something intrinsic to the model of not.
Model generally should be completely separated from controllers and thus able to stand on its own (and be used on its own). So if this is something that is only related to the model, it probably should be there; if it is just one (of many use cases), than controller is probably more appropriate.
However considering your example
$generate = $this->model->generate(); //call generate from controller
$this->model->current_stock($generate);
if you find yourself operating only on some other (=not self) object and mutating its state, than it is generally better to let the model handle it, because if you do it from outside you often make assumptions about the internal workings of the object. Not mentioning that the code doesn't seem very intention revealing… what is being generated?
Thus I find something like
$this->model->generate_current_stock();
more appropriate. Or even hide it completely (if it is not generated yet, it will be generated).
$this->model->current_stock();
Upvotes: 1