Welcome Guest, Not a member yet? Register   Sign In
New to code igniter having a hard time with instantiating classes
#1

[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.
#2

[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
}
#3

[eluser]dbug[/eluser]
That works now just not sure if its best practices for helper classes and CI.
#4

[eluser]Bart v B[/eluser]
ZIPPP... Wrong answer..
Your writing a helper..

Code:
&lt;?php
function __construct()
{
   parent::__construct();
   $CI =& get_instance();
   $CI->load->library('database');
}
#5

[eluser]danmontgomery[/eluser]
[quote author="Bart v B" date="1312863341"]ZIPPP... Wrong answer..
Your writing a helper..

Code:
&lt;?php
function __construct()
{
   parent::__construct();
   $CI =& get_instance();
   $CI->load->library('database');
}
[/quote]

No...

Code:
function __construct()
{
    parent::__construct();
    $this->load->database();
}

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');
$this->catalog->get_catalog();

http://ellislab.com/codeigniter/user-gui...lpers.html
http://ellislab.com/codeigniter/user-gui...aries.html




Theme © iAndrew 2016 - Forum software by © MyBB