How do I access my Models in CodeIgniter? - php

I'm trying to get familiar with CI and I have run into a problem while trying to implement my Model. I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$OrderModel
Filename: controllers/home.php
Line Number: 12
My assumption was that I was breaking some convention with the naming of the Model. If
I change the line in question and call the model using all lower case:
$data['query'] = $this->ordermodel->get_all_workorder_names();
Nothing is returned to the view.. blank page; no source, no error.
Here is my model:
<?php
class OrderModel extends Model{
function OrderModel()
{
parent::Model();
$db = $this->load->database();
}
function get_all_workorder_names()
{
$this->db->select('name');
$query = $this->db->get('WorkOrder');
return $query->result();
}
}
?>
This is the calling controller:
<?php
class Home extends Controller{
function Home()
{
parent::Controller();
$this->load->model('ordermodel');
$this->load->helper('url');
}
function index()
{
$data['query'] = $this->OrderModel->get_all_workorder_names();
$this->load->view('Header');
//$this->load->view('Home',$data);
$this->load->view('Footer');
}
}
?>
What am I doing wrong? A side question: Is there a debugger available for PHP in Eclipse?

Use $this->ordermodel. The variable name will match whatever you use with load->model()

For further reading, you can check out CodeIgniter's User Guide entry on loading models here.

Related

Message: Undefined property: Material_model::$load Filename: libraries/Form_validation.php

I am a newbie to Codeigniter and trying to build a service registration page. Not just the page but the whole website has this error.
$autoload['model'] = array('User_model','Material_model');
$autoload['libraries'] = array('database','form_validation');
class services extends CI_Controller {
public function service1()
{
$this->form_validation->set_rules('type','Type',trim|required);
$this->form_validation->set_rules('area','Area',trim|required);
$this->form_validation->set_rules('quantity','Quantity',trim|required);
$this->form_validation->set_rules('details','Details',trim|required);
$this->form_validation->set_rules('name','Name',trim|required);
$this->form_validation->set_rules('number','Number',trim|required);
$this->form_validation->set_rules('location','Location',trim|required);
if($this->form_validation->run()==FALSE){
$data['main_view']="page_view";
$this->load->view('layouts/main',$data);
} else{
$this->user_model->create_user();
redirect('/');
}
I'm getting this error in user model from material model. Th
Actually, you should load the model in your parent construct.
For example:
class services extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('your_directory_model, 'your_initialize_model');
}
}
And then, if you want to use your model. you can build code like i write below in your method.
$this->your_initialize_model->your_model_method();

Why is my load->view giving me an undefined property in CodeIgniter?

I haven't used CodeIgniter in over a year. I remember it was useful for quick, simple projects but I seem to have fallen at the first hurdle here. I can't seem to load my default view. Here is the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
private $data = array();
public function __constructor() {
parent::__construct();
}
public function home() {
$this->load->view('header');
$this->load->view('nav');
$this->load->view('home');
$this->load->view('footer');
}
}
This gives me:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$load
Filename: controllers/home.php
Line Number: 13
But I can't figure out why.
In my config I have set 'html', 'url' and 'form' to autoload. And my routes defaults correctly to 'home'. It's kinda frustrating because I know it's something really simple that I'm forgetting here.
Your __constructor is wrong. Use __construct instead of __constructor
public function __construct()
{
parent::__construct();
}
Your method has same name with the view you are calling which is home

Getting error in loading model in controller in codeigniter Message: Undefined property: Cart::$load

I am a beginner in codeigniter and currently I am working on a shopping cart, taking help from http://net.tutsplus.com/tutorials/php/how-to-build-a-shopping-cart-using-codeigniter-and-jquery/ tutorial. I am using codeigniter 2.1.3.
I am getting an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Cart::$load
Filename: controllers/cart.php
Line Number: 7
Fatal error: Call to a member function model() on a non-object in D:\xampp\htdocs\ci\application\controllers\cart.php on line 7
Can someone please tell me why it is not working?
The name of my controller is cart.php
<?php
class Cart extends CI_Controller {
public function Cart()
{
//parent::CI_Controller(); // We define the the Controller class is the parent.
$this->load->model("cart_model"); // Load our cart model for our entire class
}
public function index()
{
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
}
?>
and my model is cart_model.php
<?php
class Cart_model extends CI_Model{
//public function _construct(){
//parent::_construct();
//}
public function retive_products(){
$query = $this->db->get("products");
return $query->result_array();
}
}
/* End of file cart_model.php */
/* Location: ./application/models/cart_model.php */
?>
Codeigniter 2.1.3 is intended to support PHP 5.2.4 and newer.
Change the class constructor:
<?php
class Cart extends CI_Controller {
public function __construct()
{
parent::__construct();
}
instead of
public function cart()
{
parent::CI_Controller();
Try to review your code. You calling the retrieve_products function on model but in model you have retive_products function .
Controller
public function index() {
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
Model
public function retive_products() {
$query = $this->db->get("products");
return $query->result_array();
}

Variable in php class which can be used in all its functions

I want to declare a variable in a php class which can be used in all its functions. I am using the codeigniter framework & to have to pass data like base_url etc in each of the functions. Also I need all of such data in a single variable. For example $data['base_url']=base_url(), $data['title']="My_Site_title" etc & then send only a single variable $data (this is a common thing in codeigniter).
As per this question, I modified my code to:
class Search extends CI_Controller {
function Search() {
parent::__construct();
$this->load->model('student_model');
$this->load->helper('form');
$this->load->helper('url');
$this->output->enable_profiler(TRUE);
$this->data['base_url']=base_url(); //gives an error here
}
function index() {
$this->load->view('search_view', $this->data);
}
}
But still it gives an error at the marked place saying Undefined variable: data and Fatal error: Cannot access empty property in ..\search.php on line 16.
Here you go:
class Search extends CI_Controller {
protected $data = array();
function Search() {
parent::__construct();
$this->load->model('student_model');
$this->load->helper('form');
$this->load->helper('url');
$this->output->enable_profiler(TRUE);
$this->data['base_url']=base_url();
}
function index() {
$this->load->view('search_view', $this->data);
}
}
More about class properties in php: http://php.net/manual/en/language.oop5.properties.php

Message: Undefined variable: in View page

I am still new at PHP and the MVC concept. I have been trying to duplicate the CI News Tutorial(http://codeigniter.com/user_guide/tutorial/news_section.html), while using my own database and rewriting the code.
I have been unsuccessful.
Here is my controller:
class Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
$this->load->model('structures_model');
}
public function structures()
{
$this->load->model('structures_model');
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$data['structures_all'] = $this->structures_model->get_structures();
$this->load->view('templates/header', $data);
$this->load->view('templates/navmain', $data);
$this->load->view('structures', $data);
$this->load->view('templates/footer', $data);
}
}
Here is my model (structures_model)
<?php
class Structures_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_structures()
{
$query = $this->db->get('structures');
return $query->result_array();
}
}
And my view:
<?php foreach ($structures_all as $structures_info): ?>
<h2>Structures</h2>
<div id="main">
<?php echo $structures_info['str_name'] ?>
</div>
<?php endforeach ?>
The error im getting is the common :
A PHP Error was encountered<
Severity: Notice
Message: Undefined variable: structures_all
Filename: main/structures.php
Line Number: 2
I am at a loss. I have looked at all the similar errors people have gotten, but can't figure out why exactly the structure_all array is not being defined. Shouldn't it get created in the controller function where I set :
$data['structures_all'] = $this->structures_model->get_structures();
What am I missing?
The best way to debug this would be to assign $data['structures_all'] a definite array value, say: $data['structures_all'] = array('foo' => 'bar');
Is the $structures_all variable available in the view now? If it is available, you know that $this->structures_model->get_structures(); is returning null.
Do you have a table in your database called structures?
Are you sure your database connection details are filled out in config/database.php?
Do you have php error reporting set to all? There might be hidden messages... call error_reporting(E_ALL); in the constructor of your controller.
Also try echoing: $this->db->last_query(); to verify your query is being constructed the same way you tried in phpmyadmin...
Hopefully this puts you on the right track.

Categories