[SOLVED] $this->db->insert() adds all global fields from controller |
[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: 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: 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 Code: INSERT INTO user (id, user_name, real_name, role_id) VALUES the code for $this->User_Model->insert_user($user) is simply Code: function insert_user($user) { any insights on this issue would be appreciated thanks!
[eluser]indocoder[/eluser]
is it CI fault? you DO have base_url and base_uri at your User class properties. don't you?
[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)?
[eluser]wiredesignz[/eluser]
CI does this so your models have access to loaded controller objects / libraries etc. How do you think it should work?
[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...
[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.
[eluser]yarinb[/eluser]
Isn't that what I'm doing here? Code: function submit_add() { User_Model class has only the fields which I need... EDIT: just saw your edit we're to fast
[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();
[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
[eluser]yarinb[/eluser]
I found the answer to my problem (exactly as I described) here: How to filter input array |
Welcome Guest, Not a member yet? Register Sign In |