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

[eluser]Eric Barnes[/eluser]
[quote author="sqwk" date="1310540466"]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();
[/quote]

Yes but I am willing to bet (and could be wrong) if you var_dump($this) before your foreach you will see you are actually looping over tons of data. Not just your two vars in this example.


Messages In This Thread
Model best practices - by El Forum - 07-11-2011, 09:19 PM
Model best practices - by El Forum - 07-11-2011, 10:28 PM
Model best practices - by El Forum - 07-12-2011, 07:03 AM
Model best practices - by El Forum - 07-12-2011, 07:11 PM
Model best practices - by El Forum - 07-12-2011, 07:42 PM
Model best practices - by El Forum - 07-12-2011, 07:50 PM
Model best practices - by El Forum - 07-12-2011, 07:51 PM
Model best practices - by El Forum - 07-12-2011, 08:01 PM
Model best practices - by El Forum - 07-12-2011, 08:08 PM
Model best practices - by El Forum - 07-12-2011, 08:11 PM
Model best practices - by El Forum - 07-12-2011, 08:29 PM
Model best practices - by El Forum - 07-12-2011, 08:30 PM
Model best practices - by El Forum - 07-12-2011, 08:35 PM
Model best practices - by El Forum - 07-12-2011, 08:39 PM
Model best practices - by El Forum - 07-13-2011, 11:12 PM
Model best practices - by El Forum - 07-14-2011, 01:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB