Welcome Guest, Not a member yet? Register   Sign In
Model best practices
#1

[eluser]sqwk[/eluser]
Code:
// Just passing the whole lot to the create function
$this->my_model->create($data);

Is there any point/advantage in using a setter functions in a model instead of passing data directly to a create/update function?

Code:
// Setting data though predefined setter functions for protected vars.
$this->my_model->set_var1($var1)
               ->set_var2($var2)
               ->create();

// The create function would then loop through the $this object and set the various vars.
foreach ($this as $variable => $value)
    $this->db->set($variable, $value);
$this->db->insert('table');
#2

[eluser]CodeIgniteMe[/eluser]
You benefit from encapsulation and polymorphism. You can do this if you prefer, but use a property instead of using $this as an array.
Code:
foreach($this->variables as $key=>$value)
$this->db->set($key,$value);
$this->db->insert('table');
#3

[eluser]sqwk[/eluser]
So for the model you would put all the variables in one 'super-array'? Or am I misunderstanding something?
#4

[eluser]CodeIgniteMe[/eluser]
Yes, you can use a 'property' of the model to stand as a container for all your arrays.
And one more note, always clean your variable after you are done with it.
Code:
foreach($this->variables as $key=>$value)
$this->db->set($key,$value);
$this->db->insert('table');

$this->variables = array(); // clean the variable so that it can be reused with a new set of arrays.
#5

[eluser]sqwk[/eluser]
So why should I use a parent-property instead of looping over $this directly? Security? Faster?
#6

[eluser]CodeIgniteMe[/eluser]
Quote:So why should I use a parent-property instead of looping over $this directly? Security? Faster?

Because $this is an object, and not an array, if you make $this as an array, the object instance will be overwritten.
And also, using a 'parent-property' is the proper way to store values, etc.
#7

[eluser]CodeIgniteMe[/eluser]
And I just want to know, how would you implement to use $this as an array in the model class?
#8

[eluser]sqwk[/eluser]
The array is not the problem. foreach handles objects as well. Nothing is getting overwritten, it is just looping though.
Ignore the explicit setters… simplified example.

Code:
class A_Model extends CI_Model {

    private $var1;
    private $var2;

    public function __construct() {
        parent::__construct();
    }

    public function set_var1($var1) {
        $this->var1 = $var1;
        return $this;
    }
    public function set_var2($var2) {
        $this->var2 = $var2;
        return $this;
    }

    public function create() {
        foreach ($this as $var => $value)
            $this->db->set($var, $value);
        $this->db->insert('table');
        // Should really check if everything went OK, but hey…
    }

}

// Controller
$this->load->model('a_model');
$this->users_model->set_var1('foo')->set_var2('bar')->create();
#9

[eluser]CodeIgniteMe[/eluser]
Oh, I'm sorry, I was thinking you meant it like this
Quote:
Code:
class A_Model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    public function set_var1($var1) {
        $this['var1'] = $var1; // using $this as array
        return $this;
    }
    public function set_var2($var2) {
        $this['var2'] = $var2; // using $this as array
        return $this;
    }

    public function create() {
        foreach ($this as $var => $value)
            $this->db->set($var, $value);
        $this->db->insert('table');
        // Should really check if everything went OK, but hey…
    }

}
#10

[eluser]CodeIgniteMe[/eluser]
And may I ask, what would be the output of $var here:
Code:
public function create() {
        foreach ($this as $var => $value)
            $this->db->set($var, $value);
        $this->db->insert('table');
        // Should really check if everything went OK, but hey…
    }
sorry for asking, never had in mind that objects can also be looped like that.




Theme © iAndrew 2016 - Forum software by © MyBB