New to code igniter having a hard time with instantiating classes |
[eluser]dbug[/eluser]
I am new to code ignitor and mvc in general. I am having an issue when I try to use a class inside another class to access their model I get a database error. --------------- A PHP Error was encountered Severity: Notice Message: Undefined property: Catalog_helper::$db Filename: core/Model.php Line Number: 50 Fatal error: Call to a member function query() on a non-object in /opt/bitnami/apache2/htdocs/codeigniter/application/models/catalog_model.php on line 11 ---------------- Code example of what I am trying to do. I might be going about it the incorrect way. helper class: class Catalog_helper extends CI_Controller { function __construct() { parent::__construct(); $this->view_data['base_url'] = base_url(); $this->load->model('Catalog_model'); } function get_catalog(){ // query catalog model return data to view $catalog_data = $this->Catalog_model->return_catalog_data(); $data["json"] = json_encode($catalog_data); echo $data["json"]; } } Catalog model: class Catalog_model extends CI_Model { function __construct() { parent::__construct(); } function return_catalog_data(){ $query_str = "SELECT * FROM catalog"; $result = $this->db->query($query_str); return $result->result_array(); } } class using helper: class Inventory extends CI_Controller { function __construct() { parent::__construct(); $this->view_data['base_url'] = base_url(); $this->load->model('Inventory_model'); } function test_helper(){ $this->load->helper('Catalog_helper'); $catalog = new Catalog_helper; //var_dump($catalog); $catalog->get_catalog(); } } What could be causing this php error? Thanks if you have any advice.
[eluser]dbug[/eluser]
Seemed to be getting closer: <?php class Catalog_model extends CI_Model { function __construct() { parent::__construct(); $this->load->database(); <--- needs to load databse }
[eluser]dbug[/eluser]
That works now just not sure if its best practices for helper classes and CI.
[eluser]Bart v B[/eluser]
ZIPPP... Wrong answer.. Your writing a helper.. Code: <?php
[eluser]danmontgomery[/eluser]
[quote author="Bart v B" date="1312863341"]ZIPPP... Wrong answer.. Your writing a helper.. Code: <?php No... Code: function __construct() There's no reason to access the CI object indirectly from a model except when trying to load another model. Also, database is loaded directly, not through library(). More to the point, helpers aren't classes, they are a collection of functions. If you are trying to include your own class that isn't a model, it should be a library, and you should be using it like any other. Code: $this->load->library('catalog'); http://ellislab.com/codeigniter/user-gui...lpers.html http://ellislab.com/codeigniter/user-gui...aries.html |
Welcome Guest, Not a member yet? Register Sign In |