Welcome Guest, Not a member yet? Register   Sign In
Undefined property when accessing model class
#1

[eluser]Unknown[/eluser]
hi, please help me with this problem

Code:
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
    }
}


Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Test::$category_model

Filename: controllers/test.php

Line Number: 16
#2

[eluser]gyo[/eluser]
Well there must surely be an error in your coding. Smile

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

[eluser]mddd[/eluser]
This problem was discussed only yesterday. Check this reply for information on the 'loading model in a model' situation.
#4

[eluser]gyo[/eluser]
From the provided example it doesn't look like calling a model from a model. :/
#5

[eluser]mddd[/eluser]
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

[eluser]gor1lla[/eluser]
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

Code:
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

[eluser]gor1lla[/eluser]
i did a print_r($this), below is the output.
i didn't see any model loaded inside, why is it so?

Code:
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

[eluser]neo.cepot[/eluser]
[quote author="HanBurger" date="1282318483"]hi, please help me with this problem

Code:
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
    }
}


Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Test::$category_model

Filename: controllers/test.php

Line Number: 16
[/quote]

I've tried your codes in my localhost server, and there's no error found...
#9

[eluser]gor1lla[/eluser]
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

[eluser]gor1lla[/eluser]
anyone have any suggestion?




Theme © iAndrew 2016 - Forum software by © MyBB