Welcome Guest, Not a member yet? Register   Sign In
Active record in library
#1

[eluser]codex[/eluser]
Maybe I'm going about this the wrong way, but I'm trying to make a library for a mailbox and in this library I'm making several queries. I can't use $this->db because I get this error 'Call to a member function on a non-object', which is quite logical. So I'm trying to load the database class in order to use it, but it won't work.

Isn't it possible to use the class inside the library, or am I doing this wrong?

Code:
class CI_Mailbox {

    function CI_Mailbox()
    {    
        $this->CI =& get_instance();
        $this->db = $this->CI->load->database();
        log_message('debug', "Mailbox Class Initialized");
    }

// Use it like this
function set_message_status($user_id, $mail_id)
    {
        $this->db->set('mail_status', 1);
        $this->db->set('mail_date_read', date('Y-m-d H:i:s'));
        $this->db->where('mail_id', $mail_id);
        $this->db->update('mailbox');

        return TRUE;
    }
}
#2

[eluser]codex[/eluser]
[quote author="codex" date="1193601009"]Maybe I'm going about this the wrong way, but I'm trying to make a library for a mailbox and in this library I'm making several queries. I can't use $this->db because I get this error 'Call to a member function on a non-object', which is quite logical. So I'm trying to load the database class in order to use it, but it won't work.

Isn't it possible to use the class inside the library, or am I doing this wrong?

Code:
class CI_Mailbox {

    function CI_Mailbox()
    {    
        $this->CI =& get_instance();
        $this->db = $this->CI->load->database();
        log_message('debug', "Mailbox Class Initialized");
    }

// Use it like this
function set_message_status($user_id, $mail_id)
    {
        $this->db->set('mail_status', 1);
        $this->db->set('mail_date_read', date('Y-m-d H:i:s'));
        $this->db->where('mail_id', $mail_id);
        $this->db->update('mailbox');

        return TRUE;
    }
}
[/quote]


Duh, should ofcourse be
Code:
function CI_Mailbox()
    {    
        $this->CI =& get_instance();
        $this->CI->load->database();
        log_message('debug', "Mailbox Class Initialized");
    }

and
Code:
$this->CI->db->set('mail_status', 1);

Right?




Theme © iAndrew 2016 - Forum software by © MyBB