Inserting A Model Into a Database? |
[eluser]Eric Abruzzese[/eluser]
Hello, I'm relatively new to CodeIgniter, so I'm still getting used to "the CodeIgniter way", so bear with me. I have an application called Tiquette, and issue-tracking system. I am trying to minimize code duplication as much as possible, so I created models for all of my database tables. As an example, I have a model for the users who can log into the system. Naturally, they have fields like: username, password, full name, email address, phone number, notes, "is-logged-in", last login, permissions, etc. Code: class Tiquette_user extends Model { If I'm not mistaken, I should be able to do the following, according to the documentation on the Active Record class: Code: $CI->db->insert('rts_users',$tiquette_user_object); Obviously, I'm doing something wrong, because when I try that, I get this error: "You must use the "set" method to update an entry." It may be important to note that some of the fields in the model are never set (should I set them to null?) Thank you for your assistance, Eric Abruzzese
[eluser]Cro_Crx[/eluser]
That error you're getting would occur if the $tiquette_user_object variable is null or not set. It's complaining that there's nothing to insert. The error is sort of worded funny, I think it's worded like that because, you could do this instead Code: $CI->db->set($tiquette_user_object); And nothing is actually being set at the moment so it assumes you've missed out the set method. Hope this helps.
[eluser]Eric Abruzzese[/eluser]
Thanks for the quick reply. I have temporarily replaced the insert() with a print_r() to display the contents of my $tiquette_user_object. It gives me: Code: Tiquette_user Object Etc... Obviously that doesn't match my database table. Is there a built-in way to grab only the first few fields? Am I doing something wrong? Thanks for all the help! Eric Abruzzese
[eluser]Cro_Crx[/eluser]
Looks like $tiquette_user_object is being assigned to the current object ($this). You're not doing $tiquette_user_object = $this; anywhere are you ?
[eluser]Eric Abruzzese[/eluser]
I see what you mean. Here's my function I'm calling it from in the controller: Code: function add_user() EDIT: I should probably clarify that add_user simply takes it's parameter and passes it to insert() (or, as is the case right now, print_r()). The last line in that function I don't think is correct. Ideas? Thanks!
[eluser]Eric Abruzzese[/eluser]
I'm dumb. Being a Java programmer by day, I set all my variables in my model to private... hence, no visibility... hence they cannot be accessed by insert(). Thanks though! |
Welcome Guest, Not a member yet? Register Sign In |