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
Related
When autoloading classes, the following runs without a problem:
<?php
namespace App\Resources;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
How does this work? I never imported the Controller class:
<?php
namespace App\Resources;
use App\Resources\Controller;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
If you use non-qualified class name (without the namespace), PHP assumes you mean the current namespace. The code above works because both Home and Controller are in the same namespace App\Resources.
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');
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.
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.
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