Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] $this->db->insert() adds all global fields from controller
#1

[eluser]yarinb[/eluser]
Hello CodeIgniters,

I have a weird problem saving a model object to the database using $this->db->insert('table', $object);

I have a couple of local variables in the controller, defined in the constructor like:
Code:
controller:
function User() {
        // Set the base URL
        $this->base_url = $this->CI->config->site_url().'/'.$this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'both');
        $this->base_uri = $this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'leading');
        
        // more controller setups here
        ...
    }

and in the same controller, I have a insert() function which strips data from $_POST and inserts the new object into the database:

Code:
also in controller:
function submit_add() {
        $user = new User_Model();
        $user->user_name = $this->input->post('user_name');
        $user->real_name = $this->input->post('real_name');
        $user->role_id = $this->input->post('role_id');
        
        $this->User_Model->insert_user($user);
    
        // go to success message
        redirect('user');
    }

now for some reason, the insert fails since CI will run the following query:
Code:
INSERT INTO user (id, user_name, real_name, role_id, base_url, base_uri) VALUES
(2, 'user_name', 'real_name', 'role_id', 'http://localhost/~url', 'codeIgniter1.6.3'); (
instead of
Code:
INSERT INTO user (id, user_name, real_name, role_id) VALUES
(2, 'user_name', 'real_name', 'role_id'); (
why does CI adds ANY global vars from the controller to $user which is passed to $this->db->insert()?!

the code for $this->User_Model->insert_user($user) is simply
Code:
function insert_user($user) {
        $this->db->insert('user', $user);
    }

any insights on this issue would be appreciated Smile

thanks!


Messages In This Thread
[SOLVED] $this->db->insert() adds all global fields from controller - by El Forum - 07-14-2008, 06:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB