I have this file structure:
application
controllers
home.php
core
MY_Controller.php
models
users.php
These are the codes for each file:
home.php
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
echo "Hello World!";
}
}
MY_Controller.php
class MY_Controller extends CI_Controller {
var $user_model = null;
public function __construct() {
parent::__construct();
$this->user_model = new Users();
}
}
users.php
class Users extends CI_Model {
public function __construct() {
parent::__construct();
}
}
When I load the webpage on a browser, I get this error:
Fatal error: Class 'Users' not found in C:\%path%\application\core\MY_Controller.php on line 7.
Please help me make my custom base Controller to find my Model. Thank you!
NOTE:
The cases are as I have provided them (I heard there might be issues with case-sensitivities).
EDIT:
When I opened the log files, I found this.
ERROR - 2014-02-17 01:02:05 --> Severity: 8192 --> mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead C:\%path%\system\database\drivers\mysql\mysql_driver.php 91
Nevermind, I got it to work by either going in the config.php file and autoloading the model or by doing $this->load->model('Users');
Upper or lower case U seems to work.
Related
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
I can't instantiate my models manually on my controller using require_once, following is the problem:
Controller
<?php
require_once './application/models/portion/portioncreatormodel.php';
class Payment extends CI_Controller {
function sample() {
$pc = new PortionCreatorModel();
echo 'OK';
}
}
Model
<?php
class PortionCreatorModel extends CI_Model {
function __construct() {
parent::construct();
}
}
When I load this model using $this->load->model('portion/portionCreatorModel') it works perfectly (in controllers or other models), when I load using require_once using this very same require_once './application/models/portion/portioncreatormodel'; it works perfectly in other models.
Why doesn't it work properly on controllers? I found this in my error.log:
PHP Fatal error: Class 'CI_Model' not found in /var/www/html/myerp/igniter/application/models/portion/portioncreatormodel.php on line 3
Note: I already tried this require_once:
require_once APPPATH.'/models/portion/portioncreatormodel.php';
Please, don't tell me the only way to solve it is renaming PortionCreatorModel to Portioncreator_Model, it is already working properly on models, the problem is on controllers.
Any glues?
Try using this in your controller:
<?php
class Payment extends CI_Controller {
function sample() {
$this->load->model('PortionCreatorModel', 'pc');
// Reference by using $this->pc
echo 'OK';
}
}
Reference: CodeIgniter: Models
I am trying to add phpstorm autocomplete for codeigniter as seen in different quesiton.
The link provided: https://github.com/topdown/phpStorm-CC-Helpers
I commented out the specified files and added
* #property Test_model $test_model
to my_models.php
When I try to load Test_model.php from my controller using:
$this->load->model('Test_model');
I get:
'Unable to locate the specified class: Model.php'
If I remove the load row and try:
$this->Test_model->insert_entry()
I get:
Message: Undefined property: Test::$Test_model
Test.php:
class Test extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function modelTutorial(){
$this->Test_model->insert_entry();
}
Test_model.php
class Test_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function insert_entry(){
die('asdasd');
}
}
When creating models, you need to place the file in application/models/ and name the file in all lowercase - like yourfile_model.php
Change your file name from Test_model to test_model.php and call it on controller constructor
public function __construct(){
parent::__construct();
$this->load->model('test_model');
}
Read manual Models
I am trying to port an application's few functions from a CodeIgniter application to another existing CodeIgniter application. Both applications on themselves are working very well but when I added this thing it gives the following error:
Fatal error: Call to a member function order_by() on null in …\application\core\MY_Model.php on line 7
In this question I have removed parts unrelated to the error to simplify code.
//MY_Model.php model file
<?php
class MY_Model extends CI_Model {
protected $_order_by = '';
public function get(){
$this->db->order_by($this->_order_by);
}
}
//article_m.php model file
<?php
class Article_m extends MY_Model
{
protected $_order_by = 'pubdate desc, id desc';
}
//frontend.php controller file
<?php
class Frontend extends MY_Controller
{
function __construct()
{
$this->load->model('article_m');
}
function index()
{
$this->article_m->get();
}
}
Please help. Thank you!
whenever calling any $this->db ... you have to make sure to load your database library. Check in application\config\autoload.php for the following:
$autoload['libraries'] = array('database');
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