I'm trying out codeigniter for the first time on a MAMP deployment however I'm unable to load a model into a controller. The end result is always...
Unable to locate the model you have specified: second_model
Controller - welcome.php
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('second_model');
$this->load->view('welcome_message');
}
}
Model - second_model.php
class Second_model extends Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}
You should be extending CI_Model rather than Model
So:
class Second_model extends CI_Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}
Related
What I'm trying to do on the surface seems simple, basic OOP PHP but I just can't get it working. I have a controller class which is calling a model, that model extends another model of mine but it throws an error saying it can't find it:
Controller (Welcome.php):
public function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
public function index()
{
$this->users_model->getAll();
}
Users Model (User_model.php):
class Users_model extends Base_model
{
public function __construct()
{
parent::__construct();
}
}
Base Model (Base_model.php):
class Base_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAll($table)
{
$query = $this->db->query('Query here');
return $query;
}
}
This gives me the error Fatal error: Class 'base_model' not found in /ci/application/models/Users_model.php on line 3
Save your Base_model in application/core named as Base_model.php with following code.
class Base_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAll($table=FALSE)
{
$query = $this->db->query('Query here');
return $query;
}
}
Save User_model in application/models named as User_model.php having following code
class User_model extends Base_model
{
public function __construct()
{
parent::__construct();
}
}
Then make a controller Welcome.php in appliation/controllers having following code with extending CI_Controller
public function __construct()
{
parent::__construct();
$this->load->model('user_model');//loads user_model
}
public function index()
{
$data = $this->user_model->getAll(); //need a variable to hold return data
}
You just have to locate the Base_model in your Users_model like below code and you can access the functions of base model easily.
require('application/models/base_model.php');
class User_model extends Base_model
{
function __construct()
{
parent::__construct();
}
}
i am trying to make a core class in my codeigniter but its giving error that Unable to locate the specified class: Loader.php.
My Core class is MY_base.php and code is.
class MY_base extends CI_Controller{
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
My model Mod_practice.php code is
class Mod_practice extends CI_Model{
public function get_header(){
$query = $this->db->get('header');
$result = $query->result_array();
return $result;
}
}
My home.php ( main controller) code is
class Home extends MY_loader{
function index(){
parent::MY_base();
}
}
but when i try to run Home controller its giving me the following error
Unable to locate the specified class: Loader.php.
Where can be the error ? Thanks in advance.
You are doing something wrong. You need to construct CI_controller first.
class MY_base extends CI_Controller{
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
And now you can do this:
class Home extends MY_base{
public function __construct()
{
parent::__construct();
// Here you have access to load_header() function
}
}
Ou - and also your create MY_base and then refer to MY_loader.
may bee you can use HMVC Codeigniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
This is my model named Userhome_model.php
class Userhome_model extends CI_Model {
function Userhome_model() {
parent::__construct();
I tried to call this in my controller like
this->load->model('Userhome_model');
But i got an error like
Unable to locate the model you have specified: userhome_model
Change this to
this->load->model('Userhome_model');
this
this->load->model('userhome_model');
this will fix the issue(How to load model)
and Model should be place inside application/model
File Name - userhome_model.php
Inside userhome_model
class Userhome_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
}
Using model
this->model_name->method();
My CI version is CI2.3. I'm running this php code in my local host. I followed all the steps given there but I'm getting this error don't know why? and I changed Controller to CI_Controller. Hello world Program worked finely. This link code is not working. please need help!
you should extend model like this in codeIgniter
class Modelname extends CI_Model
{
function __construct()
{
parent::__construct();
}
}
Well actually the study guide is of old version of CI, where you used to extend your models from Model class as show in the guide. But now it has been changed. Now you have to extend it from CI_Model, same goes for Controller.
For controllers
class Employee_controller extends CI_Controller
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
and for models:
class Employee_model extends CI_Model
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
Use like this
<?php
class Employee_model extends CI_Model
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
You must create a model in the model folder like my_model.php
And create the class like
class My_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
}
Remember the class and the file should be same.
Docs http://ellislab.com/codeigniter/user-guide/general/models.html
I try to use the $layout property in my controllers, but I always get the following error:
Call to a member function nest() on a non-object
When dd() the value of this->layout, it seems like it is only a string.
Here's my code:
base.php
class Base_Controller extends Controller {
public $layout = 'layouts.common';
}
admin.php
class Admin_Controller extends Base_Controller {
public function action_index() {
$this->layout->nest('content', 'admin.home');
}
}
I probably miss something, but I can't find it in the docs
Ahhh, just found the trouble. I was ovewritting __construct to add filters, but wasn't calling the parent construct:
class Admin_Controller extends Base_Controller {
public function __construct() {
parent::__construct();
$this->filter('before', 'auth');
}
public function action_index() {
$this->layout->nest('content', 'admin.home');
}
}