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!
#2

[eluser]indocoder[/eluser]
is it CI fault? you DO have base_url and base_uri at your User class properties. don't you?
#3

[eluser]yarinb[/eluser]
I have them in the controller not in the model.
I insert to the database a $user object which holds only the fields which ARE in the user table.

Why does CI takes any local variables from the controller and assigns them to another class (the model)?
#4

[eluser]wiredesignz[/eluser]
CI does this so your models have access to loaded controller objects / libraries etc. How do you think it should work?
#5

[eluser]yarinb[/eluser]
I assumed that stripping down $_POST object into $user object which holds only the fields that I need for the insert will work as expected, but it doesn't.

since I must have base_url and base_uri in my controllers, how can I insert only the fields I choose from $_POST and not any other arbitrary fields from my controllers?

what's the best practice anyway to insert data with active record class? All tutorials and examples do the same as I do, but they don't have additional variables in the controller...
#6

[eluser]wiredesignz[/eluser]
You could create a user object variable in your model and pass that to db->insert($this->user)

EDIT:
Sorry it appears you are doing this already, I dont see the problem there should not be a cross over of data unless there is also a user object class variable also in your controller.

Using php5 you can make the model user class variable protected. This may help. Or you may need to rename the user object in the model.
#7

[eluser]yarinb[/eluser]
Isn't that what I'm doing here?
Code:
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->db->insert('user', $user);
    
        // go to success message
        redirect('user');
    }

User_Model class has only the fields which I need...

EDIT: just saw your edit Smile we're to fast Smile
#8

[eluser]wiredesignz[/eluser]
You are creating a model descendant, so it will have controller objects assigned as described above.

Try a simple User library or stdClass, no inheritance.

Example:
Code:
$User = new StdClass();

$User->attributeA = $this->input-post('A');

$this->db->insert($User);
#9

[eluser]yarinb[/eluser]
while using a standard class should work, I loose all the benefits of working with models this way since I don't have access to $this->db object from within my model.

I found a way to use db->set() in order to set the insert fields to the database. it's somewhat cumbersome but I still maintain CI's way of work.

thanks a lot for your help!

Yarin
#10

[eluser]yarinb[/eluser]
I found the answer to my problem (exactly as I described) here: How to filter input array




Theme © iAndrew 2016 - Forum software by © MyBB