Welcome Guest, Not a member yet? Register   Sign In
Inserting A Model Into a Database?
#1

[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 {
    
    /*
     * Active Directory credentials
     */
    private $ad_imported_from_active_directory;
    private $ad_active_directory_username;
    private $ad_active_directory_domain;
    
    /*
     * The unique ID generated by MySQL
     */
    private $db_user_id;
    
    /*
     * Information about the user
     */
    private $tu_full_name;
    private $tu_email_address;
    private $tu_phone_number;
    private $tu_notes;
    
    /*
     * Login information (password is only set if user is not part of
     * Active Directory)
     */
    private $tu_username;
    private $tu_password;
    
    /*
     * Session and login information.
     */
    private $tu_is_logged_in;
    private $tu_last_login;
    private $tu_permission_set;
        
        /* From here it's just getters and setters... */
}

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

[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);
$CI->db->insert('rts_users');

And nothing is actually being set at the moment so it assumes you've missed out the set method.

Hope this helps.
#3

[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
(
    [ad_imported_from_active_directory:Tiquette_user:private] =>
    [ad_active_directory_username:Tiquette_user:private] =>
    [ad_active_directory_domain:Tiquette_user:private] =>
    [db_user_id:Tiquette_user:private] =>
    [tu_full_name:Tiquette_user:private] => Eric C. Abruzzese
    [tu_email_address:Tiquette_user:private] => [email protected]
    [tu_phone_number:Tiquette_user:private] => (717) 873-6649
    [tu_notes:Tiquette_user:private] =>
    [tu_username:Tiquette_user:private] => admin
    [tu_password:Tiquette_user:private] => 3bac31d5b7ae39ce75b0bc02bade22d31cc10912
    [tu_is_logged_in:Tiquette_user:private] =>
    [tu_last_login:Tiquette_user:private] =>
    [tu_permission_set:Tiquette_user:private] =>
    [_parent_name] => Tiquette_user
    [_ci_scaffolding] =>
    [_ci_scaff_table] =>
    [config] => CI_Config Object
        (
            [config] => Array
                (
                    [base_url] => http://example.com/
                    [index_page] => index.php
                    [uri_protocol] => AUTO
                    [url_suffix] =>
                    [language] => english
                    [charset] => UTF-8
                    [enable_hooks] =>
                    [subclass_prefix] => MY_
                    [permitted_uri_chars] => a-z 0-9~%.:_\-
                    [enable_query_strings] =>
                    [controller_trigger] => c
                    [function_trigger] => m
                    [directory_trigger] => d
                    [log_threshold] => 0
                    [log_path] =>
                    [log_date_format] => Y-m-d H:i:s
                    [cache_path] =>
                    [encryption_key] =>
                    [sess_cookie_name] => ci_session
                    [sess_expiration] => 7200
                    [sess_encrypt_cookie] =>
                    [sess_use_database] =>
                    [sess_table_name] => ci_sessions
                    [sess_match_ip] =>
                    [sess_match_useragent] => 1
                    [sess_time_to_update] => 300
                    [cookie_prefix] =>
                    [cookie_domain] =>
                    [cookie_path] => /
                    [global_xss_filtering] =>
                    [compress_output] =>
                    [time_reference] => local
                    [rewrite_short_tags] =>
                    [proxy_ips] =>
                )

            [is_loaded] => Array
                (
                )

        )

    [input] => CI_Input Object
        (
            [use_xss_clean] =>
            [xss_hash] =>
            [ip_address] =>
            [user_agent] =>
            [allow_get_array] =>
            [never_allowed_str] => Array
                (
                    [[removed]] => [removed]
                    [[removed]] => [removed]
                    [[removed]] => [removed]
                    [[removed]] => [removed]
                    [[removed]] => [removed]
                    [[removed]] => [removed]
                    [] => -->
                    [ <![CDATA[
                )

            [never_allowed_regex] => Array
                (
                    [javascript\s*:] => [removed]
                    [expression\s*(\(|&\#40;)] => [removed]
                    [vbscript\s*:] => [removed]
                    [Redirect\s+302] => [removed]
                )

        )

    [benchmark] => CI_Benchmark Object
        (
            [marker] => Array
                (
                    [total_execution_time_start] => 0.82725200 1261451143
                    [loading_time_base_classes_start] => 0.82726400 1261451143
                    [loading_time_base_classes_end] => 0.83829200 1261451143
                    [controller_execution_time_( admin / add_user )_start] => 0.83835900 1261451143
                )

        )

    [uri] => CI_URI Object
        (
            [keyval] => Array
                (
                )

            [uri_string] => /admin/add_user
            [segments] => Array
                (
                    [1] => admin
                    [2] => add_user
                )

            [rsegments] => Array
                (
                    [1] => admin
                    [2] => add_user
                )

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
#4

[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 ?
#5

[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()
    {
        $this->load->model('tiquette_user');
        $this->tiquette_user->set_username('admin');
        $this->tiquette_user->set_password('my_password1');
        $this->tiquette_user->set_full_name('Eric C. Abruzzese');
        $this->tiquette_user->set_email_address('[email protected]');
        $this->tiquette_user->set_phone_number('(XXX) XXX-XXXX');
        $this->tiquette_user_manager->add_user($this->tiquette_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!
#6

[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!




Theme © iAndrew 2016 - Forum software by © MyBB