Welcome Guest, Not a member yet? Register   Sign In
Variable assignment - in controller or model? Advice please
#1

[eluser]mikertjones[/eluser]
Hello all

I have written several applications with Codeigniter and I have been niggled by a question each time - but I have just got on with it without really trying to find an answer.

In a Controller I might declare a variable which is ultimately to hold the results returned from a database query.

class MyClass extends Controller {
var $myVar;

function MyClass()
{
parent::Controller();
$this->load->model('mymodel');
}
}

I can assign the value to $this->myVar in one of 2 ways:

As a return value from a model function call as

$this->myVar = $this->mymodel->modelfn();
and in the model..

function modelfn()
{
$qry=$this->db->get('mytable');
if($qry->num_rows()>0)
{
return $qry->result();
}
}

Or within the model function itself

so, in the controller...
$this->mymodel->modelfn();

and in the model...
function modelfn()
{
$qry=$this->db->get('mytable');
if($qry->num_rows()>0)
{
$this->myVar = $qry->result();
}
}

Is one way better than the other in some way? Is there best practice? Is there a need to be consistent?

Any thoughts would be welcome.

With many thanks and best wishes

Mike Jones


Messages In This Thread
Variable assignment - in controller or model? Advice please - by El Forum - 09-11-2010, 02:43 AM



Theme © iAndrew 2016 - Forum software by © MyBB