Welcome Guest, Not a member yet? Register   Sign In
Undefined property on production code, but works on unit test
#1

[eluser]Christoffer[/eluser]
Hi,

I have a model called user_model that selects data from itself and another model called project_model. My controller dashboard.php extends from MY_Controller which loads both of these models in the constructor.

I then get the following error...

Code:
Undefined property: User_model::$project_model
Fatal error: Call to a member function fetch_projects_by_user() on a non-object in system/application/models/user_model.php on line 73

In this code...

Code:
class Dashboard extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
    }
    
    // ------------------------------------------------------------------------

    function index()
    {
        $this->user_model->fetch_user();

        $data = array(
            'page_title' => 'Dashboard',
            'team_name' => $this->user_model->team_name,
            'projects' => $this->user_model->user_projects
        );

        $this->load->view('dashboard_view', $data);
    }

}

That means project_model.php isn't initialized which is wierd in itself. But even wierder is that my unit test works wonders... controllers/tests/user.php can use $this->user_model->fetch_user(); without fault.

Code:
<?php
class User extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        
        $this->load->library('unit_test');
    }
    
    function index()
    {
        $this->unit->use_strict(TRUE);

        $this->user_model->fetch_user();
        
        ...
        
        echo $this->unit->run($this->user_model->user_projects, 'is_array', 'Get users\'s projects');
        
        echo '<p><pre>'.print_r($this->user_model->user_projects, TRUE).'</pre></p>';
    }

}

Why is the success/failure not consistent over both controllers?
#2

[eluser]pistolPete[/eluser]
Can you post the code of user_model.php?
#3

[eluser]Christoffer[/eluser]
user_model.php, the fetch_user() function...

Code:
// Fetch a user and associated information...
    function fetch_user()
    {
        if ( $this->user_is_online() )
        {
            $this->db->select('u.id AS user_id, u.team_id, u.name, u.email, u.is_admin, u.only_invited,
                               t.team_name, t.number_of_projects, t.number_of_users');
            $this->db->from('users u');
            $this->db->join('teams t', 't.id=u.team_id', 'INNER');
            $this->db->where('u.id', $this->user_is_online());
            
            $user = $this->db->get()->row_array();
            
            if ( count($user) )
            {
                // Set this object...
                $this->user_id                   = $user['user_id'];
                $this->user_is_admin             = $user['is_admin'];
                $this->user_only_invited         = $user['only_invited'];
                $this->user_email                = $user['email'];
                $this->user_real_name            = $user['name'];
                $this->team_id                   = $user['team_id'];
                $this->team_name                 = $user['team_name'];
                $this->team_number_of_projects   = $user['number_of_projects'];
                $this->team_number_of_users      = $user['number_of_users'];
                
                // Fetch projects asociated with the user...
                $this->user_projects = $this->project_model->fetch_projects_by_user($this->user_id, $this->team_id);
            }
        }
    }
#4

[eluser]Christoffer[/eluser]
Ok, now this is totally wierd...

When I added $this->load->library('unit_test'); to my dashboard.php constructor everything started working. Why?




Theme © iAndrew 2016 - Forum software by © MyBB