CodeIgniter Forums
HMVC problem...CI spits out undefined property when loading database. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: HMVC problem...CI spits out undefined property when loading database. (/thread-25547.html)



HMVC problem...CI spits out undefined property when loading database. - El Forum - 12-16-2009

[eluser]Jamongkad[/eluser]
Hi all!

My problem stems when I extend the Controller class.

//MX_Controller.php before HMVC it was MY_Controller.php

abstract class TemplateController extends Controller {
//...
}

My controllers seem to work well with the current setup. Even right down to the my module controllers. The problem starts when I try to access the DB, CI complains that $db is an undefined property. And that's when I invoke $this->load->database()! Strangely when I revert back to using Controller it works fine. I feel like this is a simple problem to lick but for the moment I'm stumped...


HMVC problem...CI spits out undefined property when loading database. - El Forum - 12-16-2009

[eluser]wiredesignz[/eluser]
Your abstract class definition is not relevant to the problem, why are you showing this?

The MX_Controller file is not actually required, you may save your controller base class in a like named application/libraries file and it will be (spl) autoloaded for you as needed.

Please explain your problem in more detail and show some real code. Make sure you use the forums [code] ... [/code] tags properly.


HMVC problem...CI spits out undefined property when loading database. - El Forum - 12-16-2009

[eluser]Jamongkad[/eluser]
I apologize I should have been a bit more clear with my intention. I don't know if I should provide any code for such a trivial operation. In a nutshell I cannot access the database library when using my custom base controller. Even when I'm inheriting from HMVC's Controller class.


HMVC problem...CI spits out undefined property when loading database. - El Forum - 12-16-2009

[eluser]wiredesignz[/eluser]
I have created code that mimics the code example you give and accessing the database ($db object) works fine.


HMVC problem...CI spits out undefined property when loading database. - El Forum - 12-16-2009

[eluser]Jamongkad[/eluser]
Now I'm kicking myself in the arse for not having the foresight to give some actual code. When I think about it...my Templater library calls an instance of CI within it's constructor. Do you think it has something to do with the error?

Code:
abstract class TemplateController extends Controller {
    
    //Configuration variables
    public $template = 'test_layout';
    public $enable_left_nav_parts = Array();

    //Shared variables for children
    public $vehData;

    public function TemplateController() {
        parent::Controller();
        $this->load->library('Templater');

        $this->template = new View($this->template);
    }
}

class View {
    
    private $view_file;
    private $JI;
    private $template_vars = Array();

    public function __construct($view_file) {
        $this->JI =& get_instance();
        $this->view_file = $view_file;
    }
    
    public function __set($var, $val) {
        $this->template_vars[$var] = $val;
    }

    public function __toString() {
        try {
            return $this->render(True);
        } catch (Exception $e) {
            return (string) $e;
        }
    }

    public function render($pass_as_value=FALSE) {
        return $this->JI->load->view($this->view_file,
                                     $this->template_vars,
                                     $pass_as_value);
    }
}



HMVC problem...CI spits out undefined property when loading database. - El Forum - 12-16-2009

[eluser]wiredesignz[/eluser]
So where are you accessing the database? I can't see any reference to the $db object in your code.


HMVC problem...CI spits out undefined property when loading database. - El Forum - 12-16-2009

[eluser]Jamongkad[/eluser]
Ok here is the offending code...at this point is where CI cries foul saying that I'm calling on a member function get on a non object. I'm stumped...I switch back to inheriting from the Controller and it works fine. But as soon as I inherit back to TemplateController things go awry.

Code:
class Test extends TemplateController {
  
    public function show_names() {
        $this->load->database();
        $data->firstname = $this->db->get('users');
        $this->load->view('read', $data);  
    }
}