Welcome Guest, Not a member yet? Register   Sign In
Message: Undefined property: Authentication::$Accounts
#1

[eluser]panos konstantinides[/eluser]
Hello all, I have a model called Accounts, in a file called Accounts.php

Code:
<?php
class Accounts extends Model
{
    function Accounts()
    {
        parent::Model();
    }
    
    function getAccount($username, $password, $account)
    {
        //TODO: Must fix query
        $this->CI =& get_instance();
        $this->CI->db->from('accounts');
        $this->CI->db->where('username',$username);
        $this->CI->db->where('password',$password);
        $this->CI->db->where('account_name', $account);
        
        return $this->CI->db->get();
    }
}

I have declared my model to be autloaded from within the autoload.php

Code:
$autoload['model'] = array('Accounts');

but when I try to access the method by doing

Code:
$query = $this->Accounts->getAccount($username, $password, $account);

I get the following error message

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Authentication::$Accounts

Filename: libraries/Authentication.php

Line Number: 15

Fatal error: Call to a member function getAccount() on a non-object in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\CodeIgniter_1.7.1\system\application\libraries\Authentication.php on line 15

Any ideas what's going on? I have played around with lower case and upper case letters but to no avail. The only thing I can think of is that the use of "db" from within the model is causing problems, since the database controller is also automatically loaded from the autoload.php. I tried to move the autoload of the model further up (before the line that loads the controllers) but still no luck. Any ideas?

Regards

Panos
#2

[eluser]panos konstantinides[/eluser]
Sorry I forgot to say that the Authentication class which calls the

Code:
$query = $this->Accounts->getAccount($username, $password, $account);

is a library, within the libraries/ folder, not sure if this makes any diffrence.

Regards

Panos
#3

[eluser]panos konstantinides[/eluser]
Right I have found the problem myself. I needed to do

Code:
$this->CI =& get_instance();
$query = $this->CI->Accounts->getAccount($username, $password, $account);

Can somebody shed some light on this?

Regards

Panos
#4

[eluser]smatakajr[/eluser]
Hey Dude

First Dont use capitals in models name

Code:
$query = $this->accounts->getAccount($username, $password, $account);

Second

When you initialize the Model above you dont need to get instance any more
SO

Remove

$this->CI =& get_instance();

and replace all

$this->CI->db->

with

$this->db->

Hope that helps

Rick
#5

[eluser]panos konstantinides[/eluser]
Hello Ricki, thank you for your tips. So what you're saying is that once I do

Code:
$this->CI =& get_instance();

I don't need to do it again throughout the application? What exactly is this doing? Is the get_instance() the whole context on which CodeIgniter is running? I just copied/pasted the code from another file and I don't fully understand what's the purpose of this. Do I need to do it exactly once in the application?

Thank you.

Regards

Panos
#6

[eluser]smatakajr[/eluser]
Hey

Lets take one of these examples from my own code


Code:
function Inbox_model(){
        parent::Model();
        $this->load->helper('url');
        $this->load->model('mail_model');
        $this->affiliates_id=$this->session->userdata('affiliates_id');
    }
    
    
    function paginate_mail($offset,$perpage)
    {
        // Return Mail File Database Dataset
        // Here we paginate from the database
        if(empty($offset)){$offset=0;}
    
        // LIMIT 20, 10 offset,page
        $query=$this->db->query('SELECT * from inbox where affiliates_id='.$this->affiliates_id.' order by date_created DESC LIMIT '.$offset.', '.$perpage);
        return $query->result_array();
            
    }

You can see that I construct as so

Code:
function Inbox_model(){
        parent::Model();

The parent::Model(); will Instantiate all the DB classes you need

So if you look at my second function in the model

Code:
$query=$this->db->query()

There is no need to use

$this->CI =& get_instance();


Be sure in the config folder in your database.php
that you have all the settings and in your autoload.php
the database class is initialized

Hope this helps

Rick!
#7

[eluser]panos konstantinides[/eluser]
It does help, thank you for your time Rick.




Theme © iAndrew 2016 - Forum software by © MyBB