Message: Unable to locate the model you have specified - codeigniter - php

my model file name is M_zwall.php :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class M_zwall extends CI_Model {
public function __construct(){
parent::__construct();
}
public function getLastWallpapers($start = 0,$end = 0)
{
$this->db->select('*');
$this->db->from('wallpapers');
$this->db->limit(10,$start);
return $this->db->get()->result();
}
}
?>
I loaded this model in my controller :
$this->load->model('m_zwall');
but I got this error :
Message: Unable to locate the model you have specified: M_zwall
updated
public function __construct(){
parent::__construct();
$this->load->model('m_zwall');
}

Try this..
$this->load->model('M_zwall');

Try to change file name M_zwall.php to m_zwall.php

Related

an error occured on file controller (codeigniter)

An uncaught Exception was encountered
Type: Error
Message: Call to undefined method Dashboard::cek_login()
Filename: C:\xampp\htdocs\crud_ajax_ci\application\controllers\Dashboard.php
Line Number: 9
Backtrace:
File: C:\xampp\htdocs\crud_ajax_ci\index.php
Line: 315
Function: require_once
Model file :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth_model extends CI_Model{
public function cek_login($username)
{
$hasil = $this->db->where('username', $username)->limit(1)->get('users');
if($hasil->num_rows() > 0){
return $hasil->row();
} else {
return array();
}
}
public function register($table, $data)
{
return $this->db->insert($table, $data);
}
}
Controller file :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->cek_login();
}
public function index()
{
$this->load->view('dashboard');
}
}
For accessing model method you have to load model class First then access Model method.
<?php
class Dashboard extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Auth_model'); // Load Model
$this->Auth_model->cek_login($username); // Load Model Method
}
public function index()
{
$this->load->view('dashboard');
}
}
?>
Note:-
Once loaded Model in Controller, you will access your model methods using an object with the same name as your Model class Name:-
$this->load->model('model_name');
$this->model_name->method();
Check here :-
For Loading Model
https://codeigniter.com/userguide3/general/models.html#loading-a-model

Unable to locate the model you have specified in CodeIgniter:

I am trying to call a model in controller in codeigniter but that give me error that to locate the model you have specified. Everything looks ok in my code but I did not get why it is not finding the model here is my model
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database("chandqki_mmusic");
}
public function insert_users_to_db($data)
{
return $this->db->insert('music_user_register', $data);
}
}
?>
and in my controller I am calling this model
class Home extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->helper('url');
$this->load->view('Home_view');
$this->load->model('register_model');
}
}
From my side its code is looking ok but May be I am missing somewhere can anybody help me regarding this. I have go through all the answers of stackoverflow and check all the whatever they say everything is I am following like that but still I am unable to solve it
Thanks
Your model file name must be ucfirst
Register_model.php
Replace
$this->load->model('register_model');
with
$this->load->model('Register_model');
R in register in caps.

Codeigniter index parameter

This is not the only controller in the folder. I already deleted index.php.
but once i add this part of a function for a shopping cart
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller
{
public function index($index = 0)
{
$data['listProduct'] = $this->mproduct->findAll();
$this->load->view('template/header.php', $data);
$this->load->view('index', $data);
}
}
it gives me this error
Message: Undefined property: Product::$mproduct
Filename: controllers/product.php
Line Number: 7
here is my model
<?php
class MProduct extends CI_Model
{
function _construct()
{
parent::_construct();
}
function findAll()
{
return $this->db->get('product')->result();
}
function find($id)
{
$this->db->where('id', $id);
return $this->db->get('product')->row();
}
}
You need to load the model first before calling it.
Also the first letter should be capitalized with rest of the name should be in lowercase.
Class names must have the first letter capitalized with the rest of
the name lowercase. Make sure your class extends the base Model class.
Refer
Add this:
$this->load->model ( 'Mproduct' );
$data['listProduct'] = $this->Mproduct->findAll();
Full code :
Controller : controllers/Product.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller {
public function index()
{
$this->load->model ( 'Mproduct' );
$data['listProduct'] = $this->Mproduct->findAll();
$this->load->view('template/header.php', $data);
$this->load->view('index', $data);
}
}
Model : models/Mproduct.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mproduct extends CI_Model {
function _construct()
{
parent::_construct();
}
function findAll()
{
echo 'model';
}
function find($id)
{
echo 'model';
}
}

Undefined property: CI_Loader::$impostazioni_model

I'm working on a calendar planning provided by [easyappointments][1] but now I'm stuck on this error by yesterday. In the main page users.php I've added the following:
<?php
require_once("application/models/impostazioni_model.php");
$this->load->model('impostazioni_model');
$this->impostazioni_model->load_colours();
?>
the require_once find correctly the file impostazioni_model.php but when I enter in the page users.php I see this error:
Fatal error: Call to a member function load_colours() on a non-object
on this line: $this->impostazioni_model->load_colours();
in the class impostazioni_model.php I've this content:
<?php if ( ! defined('BASEPATH')) exit('Direct execution not allowed.');
class Impostazioni_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function load_colours()
{
echo "print";
}
}
?>
I've followed the codeigniter documentation, in particular the class model must be in capital letter so I don't know what I'm wrong. Someone could help me out?
In Controller
Path - application/controllers
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('impostazioni_model'); //this will show "Print" word on browser.
}
function index() {
$this->impostazioni_model->load_colours();
}
}
In Model
Path - application/model
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Impostazioni_model extends CI_Model {
function __construct() {
parent::__construct();
}
function load_colours() {
echo "print";
}
}

What is wrong with my Model and Controller?

For the life of me I cannot understand why I am receiving errors with my install of CodeIgniter and this current MVC set up.
The error I see is
Fatal error: Call to undefined method Login::users_model() in /var/www/application/controllers/login.php on line 17
Controller application/controllers/login.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model()->test_user();
$this->load->view('home',$data);
}
}
Model application/models/users_model.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function test_user() {
return 'Test User #1';
}
}
View application/views/home.php
echo $testing;
No need for function bracket with model name
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model->test_user();
$this->load->view('home',$data);
}
}
Replace $data['testing'] = $this->users_model()->test_user(); with $data['testing'] = $this->users_model->test_user(); in your controller.
Simply load your model inside the constructor
public function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
Then simply call the relevant functions inside any controller functions like this.
public function index()
{
$data = '';
$data['testing'] = $this-> users_model-> test_user();
$this->load->view('home',$data);
}

Categories