ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Undefined property when accessing model class

August 20, 2010 7:34am

Subscribe [6]
  • #1 / Aug 20, 2010 7:34am

    HanBurger

    1 posts

    hi, please help me with this problem

    class Test extends Controller 
    {
    
        function Test()
        {
            parent::Controller();
        }
        
        function index() 
        {
            $this->load->model('category_model');
            $this->category_model->get(1);
        }
    }
    
    class Category_model extends Model 
    {
        function Category_model() 
        {
            parent::Model();
        }
      
        function get($id) 
        {
            // some function to get data from database
        }
    }
    A PHP Error was encountered
    
    Severity: Notice
    
    Message: Undefined property: Test::$category_model
    
    Filename: controllers/test.php
    
    Line Number: 16
  • #2 / Aug 20, 2010 10:29am

    gyo

    170 posts

    Well there must surely be an error in your coding.  😊

    Make sure the the model exists in the folder (and extend CI_Model if you’re using CI 2.0), give a better look at the wonderful user guide for more info, since this is a very basic thing and it should work without problems.

    Welcome to CodeIgniter!

  • #3 / Aug 20, 2010 10:35am

    mddd

    635 posts

    This problem was discussed only yesterday. Check this reply for information on the ‘loading model in a model’ situation.

  • #4 / Aug 20, 2010 11:34am

    gyo

    170 posts

    From the provided example it doesn’t look like calling a model from a model.  :/

  • #5 / Aug 20, 2010 1:04pm

    mddd

    635 posts

    I’m sorry, I didn’t read well enough. It should work as you do it.. I can’t think why the model wouldn’t load.
    Maybe you can do a “print_r($this);” after loading the model? To see if it is loaded and what variable name it has!

  • #6 / Aug 21, 2010 2:39am

    gor1lla

    6 posts

    me n hanburger are from the same team.

    just to clarify on the code in the first post
    the Test Controller class and the Category_model class are inside 2 separate files

    class Test extends Controller 
    {
    
        function Test()
        {
            parent::Controller();
        }
        
        function index() 
        {
            $this->load->model('category_model');
            $this->category_model->get(1);
        }
    }
    /* Location: ./system/application/controllers/test.php */
    
    -----------------------------------------------------------------------------------
    
    class Category_model extends Model 
    {
        function Category_model() 
        {
            parent::Model();
        }
      
        function get($id) 
        {
            // some function to get data from database
        }
    }
    /* Location: ./system/application/models/category_model.php */
  • #7 / Aug 21, 2010 2:43am

    gor1lla

    6 posts

    i did a print_r($this), below is the output.
    i didn’t see any model loaded inside, why is it so?

    Test Object
    (
        [_ci_scaffolding] => 
        [_ci_scaff_table] => 
        [config] => CI_Config Object
            (
                [config] => Array
                    (
                        // hidden
                    )
    
                [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.79056900 1282368477
                        [loading_time_base_classes_start] => 0.79060700 1282368477
                        [loading_time_base_classes_end] => 0.80118600 1282368477
                        [controller_execution_time_( test / index )_start] => 0.80131200 1282368477
                    )
    
            )
    
        [uri] => CI_URI Object
            (
                [keyval] => Array
                    (
                    )
    
                [uri_string] => test/index
                [segments] => Array
                    (
                        [1] => test
                        [2] => index
                    )
    
                [rsegments] => Array
                    (
                        [1] => test
                        [2] => index
                    )
    
                [config] => CI_Config Object
                    (
                        [config] => Array
                            (
                               // hidden
                            )
    
                        [is_loaded] => Array
                            (
                            )
    
                    )
    
            )
    
        [output] => CI_Output Object
            (
                [final_output] => 
                [cache_expiration] => 0
                [headers] => Array
                    (
                    )
    
                [enable_profiler] => 
            )
    
        [lang] => CI_Language Object
            (
                [language] => Array
                    (
                    )
    
                [is_loaded] => Array
                    (
                    )
    
            )
    
        [router] => CI_Router Object
            (
                [config] => CI_Config Object
                    (
                        [config] => Array
                            (
                                // hidden
                            )
    
                        [is_loaded] => Array
                            (
                            )
    
                    )
    
                [routes] => Array
                    (
                        [scaffolding_trigger] => 
                    )
    
                [error_routes] => Array
                    (
                    )
    
                [class] => test
                [method] => index
                [directory] => 
                [uri_protocol] => auto
                [default_controller] => welcome
                [scaffolding_request] => 
                [uri] => CI_URI Object
                    (
                        [keyval] => Array
                            (
                            )
    
                        [uri_string] => test/index
                        [segments] => Array
                            (
                                [1] => test
                                [2] => index
                            )
    
                        [rsegments] => Array
                            (
                                [1] => test
                                [2] => index
                            )
    
                        [config] => CI_Config Object
                            (
                                [config] => Array
                                    (
                                        // hidden
                                    )
    
                                [is_loaded] => Array
                                    (
                                    )
    
                            )
    
                    )
    
            )
    
        [load] => CI_Loader Object
            (
                [_ci_ob_level] => 1
                [_ci_view_path] => // hidden
                [_ci_is_php5] => 1
                [_ci_is_instance] => 
                [_ci_cached_vars] => Array
                    (
                    )
    
                [_ci_classes] => Array
                    (
                    )
    
                [_ci_loaded_files] => Array
                    (
                    )
    
                [_ci_models] => Array
                    (
                        [0] => category_model
                    )
    
                [_ci_helpers] => Array
                    (
                    )
    
                [_ci_plugins] => Array
                    (
                    )
    
                [_ci_varmap] => Array
                    (
                        [unit_test] => unit
                        [user_agent] => agent
                    )
    
            )
    
        [db] => CI_DB_mysql_driver Object
            (
                // hidden
            )
    
    )
  • #8 / Aug 21, 2010 3:09am

    neo.cepot

    4 posts

    hi, please help me with this problem

    class Test extends Controller 
    {
    
        function Test()
        {
            parent::Controller();
        }
        
        function index() 
        {
            $this->load->model('category_model');
            $this->category_model->get(1);
        }
    }
    
    class Category_model extends Model 
    {
        function Category_model() 
        {
            parent::Model();
        }
      
        function get($id) 
        {
            // some function to get data from database
        }
    }
    A PHP Error was encountered
    
    Severity: Notice
    
    Message: Undefined property: Test::$category_model
    
    Filename: controllers/test.php
    
    Line Number: 16

    I’ve tried your codes in my localhost server, and there’s no error found…

  • #9 / Aug 21, 2010 6:44am

    gor1lla

    6 posts

    the issue happen on the hosting server
    on my localhost, it is working fine

    as far as i know, the hosting server is running php 5
    on my localhost, i m running php 5 as well

    is there any setting inside php.ini that can result in this issue?
    or is there any area i can go and investigate to determine the source of the problem?

    any help is greatly appreciated.

  • #10 / Aug 22, 2010 4:54am

    gor1lla

    6 posts

    anyone have any suggestion?

  • #11 / Aug 22, 2010 6:08am

    pbarney

    15 posts

    You’re leaving out some code that might be important to diagnose the problem.

    The error says there’s a problem on line 16, but there are only 14 lines that you show us.

    Please post the actual file.

  • #12 / Aug 22, 2010 7:31am

    gor1lla

    6 posts

    You’re leaving out some code that might be important to diagnose the problem.

    The error says there’s a problem on line 16, but there are only 14 lines that you show us.

    Please post the actual file.

    i delete some of the code that was commented.

    i have attached the file and now the error is on line 13
    when i am trying to access the $this->category_model->get(1);

  • #13 / Aug 22, 2010 3:23pm

    pbarney

    15 posts

    There is nothing wrong with the code you’re giving us. There has to be more to it than that. Based on what you’ve shown us, there’s nothing that anyone here can tell you. The problem is not with the code you’ve shown, but with some other part that we’re not seeing.

  • #14 / Aug 22, 2010 6:56pm

    gor1lla

    6 posts

    The problem is I didn’t change any of the other files, except changing the url inside config.php.
    I only added those 2 files.

    If I changed my code to the following, there isn’t any error.

    class Test extends Controller {
        function Test()
        {
            parent::Controller();
        }
        
        function index() 
          {
            $CI =& get_instance();
            $CI->load->model('category_model');
            $CI->category_model->get(1);
        }
    }

    I did a print_r($CI) inside the Loader library’s model function, category_model is there.
    I can even call the model via $CI->category_model and no error.

    function model($model, $name = '', $db_conn = FALSE)
        {        
            if (is_array($model))
            {
                foreach($model as $babe)
                {
                    $this->model($babe);    
                }
                return;
            }
    
            if ($model == '')
            {
                return;
            }
        
            // Is the model in a sub-folder? If so, parse out the filename and path.
            if (strpos($model, '/') === FALSE)
            {
                $path = '';
            }
            else
            {
                $x = explode('/', $model);
                $model = end($x);            
                unset($x[count($x)-1]);
                $path = implode('/', $x).'/';
            }
        
            if ($name == '')
            {
                $name = $model;
            }
            
            if (in_array($name, $this->_ci_models, TRUE))
            {
                return;
            }
            
            $CI =& get_instance();
            if (isset($CI->$name))
            {
                show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
            }
        
            $model = strtolower($model);
            
            if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
            {
                show_error('Unable to locate the model you have specified: '.$model);
            }
                    
            if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
            {
                if ($db_conn === TRUE)
                    $db_conn = '';
            
                $CI->load->database($db_conn, FALSE, TRUE);
            }
        
            if ( ! class_exists('Model'))
            {
                load_class('Model', FALSE);
            }
    
            require_once(APPPATH.'models/'.$path.$model.EXT);
    
            $model = ucfirst($model);
                    
            $CI->$name = new $model();
            $CI->$name->_assign_libraries();
            
            $this->_ci_models[] = $name;
    
            // tracing 
            print_r($CI);
            $CI->category_model->get(1);
        }

    I am suspecting that the $CI is not referenced properly.
    Is $this same as $CI =& get_instance() ?
    Could it be a php.ini setting that is affecting this?

  • #15 / Sep 03, 2010 3:13pm

    Brian Loomis

    11 posts

    I can confirm that is is replicable under CI 1.7.2

    for about 90 minutes this morning I struggled with this and finally found this post. I changed all instances of $this to $CI in my controller, ran the page again, and then had another error related to $CI, HOWEVER, when I did another find and replace on $CI to $this in the controller all my code worked magically fine.

    This error persisted before the find and replace even after several apache restarts, and one restart of the laptop.  There was nothing else changed in code.  This was on a locally hosted development site so no ISP or upstream caching was responsible either.

    Brian

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases