Part of the EllisLab Network
   
 
Call to a member function get() on a non-object with embedded controllers
Posted: 07 March 2007 01:16 PM   [ Ignore ]  
Summer Student
Total Posts:  20
Joined  03-05-2007

I’m not sure if this is a bug or just something that wasn’t ever meant to work, but I’m getting a “Fatal Error: Call to a member function get() on a non-object” error when I try to do a simple database call using nested controllers.

My basic setup is that I have a generic controller that loads another controller and then loads a view that just echos the ‘answer_list’ variable.

<?
class Generic extends Controller
{
    
function Generic()
    
{
        parent
::Controller();
    
}
    
    
function index()
    
{
        
//Include answer controller
        
require_once('answer.php');
        
        
//Initialize controller
        
$answer = new Answer();
        
        
//Call display() function in answer controller
        
$data['answers_list'] = $answer->display();

        
//Load generic view
        
$this->load->view('generic_view', $data);
    
}
}
?>

And in the answer controller, I have a pretty simple display() function

class Answer extends Controller
{
    
function Answer()
    
{
        parent
::Controller();
        
$this->load->model('question_model');
    
}
    
    
function display()
    
{
        $data[
'query'] = $this->question_model->getAllAnswers();
        
        return
$this->load->view('Answer/question_index', $data, true);
    
}
}
?>

The getAllAnswers() function is in my question_model and just does a simple db->get call.

class Question_model extends Model
{
    
function Question_model()
    
{
        parent
::Model();
    
}

    
function getAllAnswers()
    
{
        
return $this->db->get('answers');
    
}
}

I’ve autoloaded the database library, so I know thats not the issue. The php error page is complaining about the “return $this->db->get(‘answers’);” call. Is this supposed to be able to work?

Profile
 
 
Posted: 08 March 2007 02:37 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  1
Joined  03-08-2007

ei i got the same problem…

i dont know if its a bug or something…..

do you know other alternatives similar to the get() function?....

im still new with this stuff, you know….

im planning to do the old way of connecting to the database but still using the codeigniter controller…

Profile
 
 
Posted: 08 March 2007 07:27 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

afaik, you can’t embed controllers like this.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 09 March 2007 10:37 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  20
Joined  03-05-2007
Shadowhand - 08 March 2007 07:27 PM

afaik, you can’t embed controllers like this.

So you mean this error I’m getting is intentional? It’s odd, because this works fine with anything that doesn’t use the database when it loads the view. For example, if I call the display() function and it does not do a database call, everything will display fine. Also, if something in that view calls a controller function that does a database hit, that will work as well. It just seems to be a problem when the view is first loaded.

Profile
 
 
Posted: 09 March 2007 02:42 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  366
Joined  04-27-2006

just search for older posts, there are always from time to time threads like
calling controller from a view, or something like that.
I think some people found ways to get this to work.
good luck!

edit:
http://codeigniter.com/forums/viewthread/47981/
read the posting from weeJames: “For those interested it is possible to use a controller from within another.”

Profile
 
 
Posted: 09 March 2007 03:14 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  20
Joined  03-05-2007

Thats the exact post that I read and based my example off of. Everything works great except for this database issue. I think he was probably right in that most people consider it the wrong way of doing things, but it seems like the best option for what I need.

Profile
 
 
Posted: 09 March 2007 04:01 PM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  366
Joined  04-27-2006

uh, sorry wink
could it be, that you have to use $CI =& get_instance(); in this model (in this special case…)?
there is also a php function that you could use… it prints all variables and objects… so you print it in your model and you can see, if the db or loader class is available… I don´t know the name of the php function now…

Profile
 
 
Posted: 09 March 2007 04:52 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  20
Joined  03-05-2007

Bummer, no dice with the get_instance() call. But I did notice something weird happening when I changed it.

When I originally had this in my model:

function getAllAnswers()
{
    
return $this->db->get('answers');
}

I was getting “Undefined Property: Question_model::$db”, but when I use the get instance call like this:

function getAllAnswers()
{
    $CI
=& get_instance();
    return
$CI->db->get('answers');
}

The error changes to “Undefined Property: Answer::$db”. It would be nice to use that PHP function you’re talking about to see if the db library is loaded, cause it pretty much looks like thats the issue here.

Profile
 
 
Posted: 09 March 2007 06:29 PM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  366
Joined  04-27-2006

hm, thats getting too complicated for me ;-)
Well, I did not found a php function for displaying all objects but
you can check, if the database is available…

      if(method_exists($this->db,‘query’))  { 
        echo “it´s ready to use!”;
      }else{
        echo “oh no, it´s not here…”;     
      }

Profile
 
 
Posted: 05 April 2007 04:42 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  1
Joined  03-19-2007

I had a similar problem when I was initially trying to establish database connectivity.  I was getting the error:

“Call to a member function get() on a non-object in…”

I found that I needed to change, within config/autoload.php the following line from:

$autoload[‘libraries’] = array();

to:

$autoload[‘libraries’] = array(‘database’);

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120447 Total Logged-in Users: 32
Total Topics: 126531 Total Anonymous Users: 3
Total Replies: 665334 Total Guests: 366
Total Posts: 791865    
Members ( View Memberlist )