Welcome Guest, Not a member yet? Register   Sign In
$this->db becomes a non-object inside model
#1

[eluser]Bryan Zera[/eluser]
Below is some cropped code from my problematic model.

The problem is the function load(). When used within the get() function, it works as it should. When used in the get_associated() function, it throws the following error:
Code:
Fatal error: Call to a member function on a non-object in /var/www/codeigniter/application/models/modelplus.php on line 83

Line 83 is the line "$this->db->where('id', $id);" inside the load() function.

My gut reaction is that the problem resides in the scope of $this, but why would the load() function work from within one of the model's function, but not another.

Any help is greatly appreciated.

Code:
<?php
    
    class ModelPlus extends Model {
    
        var $data_description;
        var $table_name;
        
        function ModelPlus() {
        
            parent::Model();
            $this->data_description = array();
            $this->table_name = get_class($this);
            
        }
            
        function get_associated($model_name) {
        
            if (!strcmp(get_class($this),$model_name))
                return false;
        
            // find the association table.  association tables are titled in the format:
            //
            // [model1]_[model2]
            //
            // The model names are always in alphabetical order in the association table
            // name.
            
            $association_table = (strcmp(get_class($this), $model_name) < 0 ?
                get_class($this) . '_' . $model_name :
                $model_name . '_' . get_class($this));
                
            $this->db->where(get_class($this) . '_id', $this->id);
            $results = $this->db->get($association_table);
            
            if ($results->num_rows()) {
            
                $retval = array();
                
                foreach ($results->result() as $r) {
                    
                    eval('$tmp_id = $r->' . $model_name . '_id;');
                    eval('$tmp = new ' . $model_name . '();');
                    
                    $tmp->load($tmp_id);                
                    $retval[] = $tmp;
                    
                }
                return $retval;
            } else {
                return null;
            }
                
        
        }

        function load($id) {
        
            if (!$id)
                return false;

            $this->db->where('id', $id);
            
            $result = $this->db->get($this->table_name);
            
            if ($result->num_rows()) {
            
                $result = $result->result();
                $result = $result[0];
                
                foreach ($result as $key=>$value) {
                    $this->$key = $value;
                }
                    
            } else
                return false;

        }        
            
    }
    
?&gt;
#2

[eluser]tonanbarbarian[/eluser]
have you loaded the database yet
either add the database to the autoload.php file
or in your constructor
Code:
$this->load->database();

the database is NOT loaded automatically in CI (thankfully)
#3

[eluser]Bryan Zera[/eluser]
Yes, database is in autoload.php
#4

[eluser]tonanbarbarian[/eluser]
are the database settings correct?
do you have active record enabled?
#5

[eluser]Seppo[/eluser]
Which is line 83?

Also, try to replace
Code:
eval('$tmp_id = $r->' . $model_name . '_id;');
eval('$tmp = new ' . $model_name . '();');

with this
Code:
$tmp_id = $r->{$model_name . '_id'};
$tmp = new $model_name();

always try to avoid "eval" calls
#6

[eluser]Bryan Zera[/eluser]
Tonan> Yes, active record is enabled. The database works just fine.

Seppo> Line 83 is the line “$this->db->where(’id’, $id);” inside the load() function.

I'll try with the alternate syntax. I wasn't aware of the syntax you posed.
#7

[eluser]Seppo[/eluser]
Which CI version are you using? Are you sure AR is enabled?
#8

[eluser]Bryan Zera[/eluser]
Problem solved. Thanks to everyone who posted suggestions.

Seppo: Thanks for that alt. syntax. I don't like using eval, but now I have a way around it.

Kids: Be sure to call the parent constructor when you extend your own classes. Your classes are orphans until you call the parent constructor and your models aren't smart enough to figure out who their parents are themselves.
#9

[eluser]Unknown[/eluser]
Well,
actually I had the same problem (1.6.1):

Before :
- Autoload.php : $autoload['core'] = array('database');
- the Scaffolding feature was working well, so the db connection I think.
But the constructor was not recognize db as a object.
"
Severity: Notice
Message: Undefined property: Festnoz::$db
Filename: controllers/festnoz.php
Line Number: 30
Fatal error: Call to a member function get() on a non-object in C:\wamp\www\CodeIgniter\system\application\controllers\festnoz.php on line 30
"

What I did :
I add [$this->load->database();] in my main controller

After
- It works well

I really thought I do not nead to load manually the db in the controller ... but only into the autoload ...




Theme © iAndrew 2016 - Forum software by © MyBB