Well, I'm newbie in CodeIgniter Framework and i'm trying building a generic Model Class. See:
class Basic_Model extends CI_MODEL {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
I want to extends all Models based on Basic_Model, like this:
class Pagina_Model extends Basic_Model {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
The problem is when i try to call "pagina_model" from a Controller a got the following error:
Fatal error: Class 'Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
If i use "basic_model" in controller everything works fine.
EDIT 1:
I created a file named MY_Basic_Model.php in "/application/core" and changed the class name to "MY_Basic_Model". But i got the error:
Fatal error: Class 'MY_Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
For this you have to create Core System Classes (this is also known as Method Overriding).
Create a file MY_Model.php in application/core/ directory which will extend the base CI_Model class:
<?php
class MY_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Now you can extend this in your models (../applicaton/models/):
<?php
class Pagina_Model extends MY_Model {
function __construct()
{
parent::__construct();
}
}
Few things to note here:
1) The class declaration must extend the parent class.
2) Your new class name and filename must be prefixed with MY_ (this item is configurable).
How to configure:
To set your own sub-class prefix, open your application/config/config.php file and look for this item:
$config['subclass_prefix'] = 'MY_';
Documentation:
https://ellislab.com/codeigniter/user-guide/general/core_classes.html
You can do it this way.
lets assume you have basic_model.php inside your model folder .
Now add the code for class Basic_Model that you have written
class Basic_Model extends CI_MODEL {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
Now make a pagina_model.php inside your model folder and add the code that you written. Just include the first line like bellow
<?php
require APPPATH.'/models/basic_model.php';//just add this line and keep rest
class Pagina_Model extends Basic_Model {
function __construct()
{
parent::__construct();
}
}
hope this will solve your problem
You can do this ... MY_ model is really good but should you wish a sub model to extend a different sub model you can always do:
require(APPPATH.'models/Other_model.php');
class New_model extends Other_Model {
In my instance Other_Model actually extends MY_model.
Related
I try to load this model named "m_cart", but it doesn't work on my controller. I got an error here "M_cart.php exists, but doesn't declare class M_cart"
Controller Part.php
<?php
class Cart extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('M_cart');
$this->load->session('cart');
}
Model m_cart.php
<?php
class Cart_model extends CI_Model{
function get_all_produk(){
$hasil=$this->db->get('produk');
return $hasil->result();
}
Controller name should be Cart.php not Part.php if you are declaring class Cart(don't forget to extend CI_Controller).
The m in m_cart should be capitalized both in the file name M_cart.php and declaration and should extend CI_Model e.g. class M_cart extends CI_Model.
If both fail to work, verify verify verify that you are editing the same copies as you are testing .etc.
Your model must be declared as a class that extends CI_Model
Class M_cart extends CI_Model
{
function get_all_produk()
{
$hasil=$this->db->get('produk');
return $hasil->result();
}
function some_other_model()
{
// some other thing
}
}
load->model('cart_model');
$this->load->session('cart');
}
//this is where your problem is coming from, you did not put the right model name. you used m_cart instead of cart_model//
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 have a model -
class Model{
public $childModel;
public function getModel($url){
$this->childModel = 'model/'.$url;
include($this->childModel);
new ModelHomeHome();
}
}
this is child model -
class ModelHomeHome extends Model{
function __construct(){
echo 'This is Home Model.';
}
}
a Controller Class -
class Controller{
public $model;
function __construct(){
$this->model = new Model();
}
}
and this is the child controller -
class ControllerHomeHome extends Controller{
function help(){
$this->model->getModel('home/home.php');
}
}
and in a page -
include(controller.php);
include(model.php);
The problem is the child model class is not initializing. Fatal error: Call to a member function getModel() on a non-object ... .
If I place the $this->model = new Model(); inside the help() function of the child controller instead of placing inside the parent controller, it works. But I want to initialize the model inside the constructor of parent controller. Please help.
Are you sure that is the path to your file. Most frameworks I have seen usually have files all over the place so I try to have constants that ensure I am in the correct parent director when retrieving files.
This works fine for me when I include the model and controller class and run the following code
include('controller.php');
include('model.php');
$home = new ControllerHomeHome();
$home->help();
outputs:
This is Home Model.
I want to create two parent controllers: one for admin and one for user site. They have to extend a regular Controller class but each of them has to do different things.
I wrote up an article showing how you do this.
http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
You need to create an __autoload() function in your config.php or directly include the base controller above the class definition.
This is pretty easy. Do the following:
Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
Open MY_Controller.php and add your multiple classes, like so:
class Admin_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
var_dump("from Admin_Parent");
}
}
class User_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
var_dump("from User_Parent");
}
}
Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php
Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:
class Adminchild extends Admin_Parent {
function __construct() {
parent::__construct();
}
function test() {
parent::test();
}
}