About CodeIgniter structure - php

I have a project based on codeigniter. I already have a model that extends some CI_Model class. class Playermodel extends CI_Model . I need to import Playermodel in one of my classes so that I can easily access database without hard coding the connection with php.
However this CI_Model class is making me trouble coz I cannot really find a file with CI_Model class. Also when I import it I get internal server error 503.
What should I do in this case and where can I find this CI_Model class?
Thanks

Your models will typically be loaded and called from within your controller functions. To load a model you will use the following function:
$this->load->model('Model_name')
The DB connection settings are stored in application/config/database.php you dont need to hardcord it int your files. Then to connect to database
$this->load->database();
Read the revelant user_guide model database

Related

In HMVC, how to load a model from another module?

on the server I can not load a model that is in another module in the HMVC, framework of the Codeigniter framework, I tried this way:
Class Contract extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Contract_model', 'contract');
$this->load->model('register/customer_model', 'customer');
$this->load->model('register/daydue_model', 'due');
}
}
Structure
In the localhost works quietly, since the server is giving this error::
Message: Unable to locate the model you have specified: Daydue_model
When you uploading on server use casesensitive it does not create problem on localhost. your model name is incorrect.
Do it like this
$this->load->model('register/daydue_model', 'due');
And rename your model name to Daydue_model and check the class name same is the name of model like:
class Daydue_model extends CI_Model

Extending own class in PHP CodeIgniter 3

I have two models: models/Categories.php and models/backend/BE_categories.php
I want BE_categories class to extend Categories, since it has some useful functions I can re-use:
Categories.php
class Categories extends CI_Model
{
...
BE_categories.php
class Be_categories extends Categories
{
...
But when $this->load->model('models/be_categories') I get the error: Unable to locate the specified class: Session.php yet it works with exending CI_Model What have I done wrong?
Just my guess, you load Session library inside some function in Categories model,and then try to use function with session library specific function inside model BE_Categories.
Add Session library in config/autoload.php or load it inside Categories model constructor, and remember to add constructor inside BE_Categories.
I also had a Controller called Categories.php, with class Categories, which is where CI was getting confused.
I had to rename models/Categories.php to models/Categories_model.php as well as the class name.
But also had to include the file when extending:
BE_categories_model.php
include('application/models/Categories_model.php');
class Be_categories_model extends Categories_model
{
...
Edit:
Thanks to commenter, might be better to to include this model in config/autoload.php instead of using PHP's include() function.

parent controll in Codeigniter 2.2 project not found

In my CI 2.2 project I want to make my parent controll with app's common functionality for use in all app and for this I create file :
application/libraries/N_Controller.php :
<?php
class N_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
But on first attempt to use it in file
application/controllers/admin/admin.php
<?php
class Admin extends N_Controller
{
public function __construct()
{
parent::__construct();
I got error:
PHP Fatal error: Class 'N_Controller' not found in /controllers/admin/admin.php on line 3, referer: http://local-ci22.com/admin/hostel/edit/15
I tried to add in application/config/autoload.php file :
$autoload['libraries'] = array( 'AppSmarty', 'AppUtils', 'N_Controller');
But it did not help. Which is the correct way ?
if you want to extend the functionality of a system class. you need to follow this recomendations.
place your extended class in /application/core, be sure that you name it exactly like the name and casing of the class created.
your class should extend from CI_Model or CI_Controller, depending on your needs.
wherever you implement your new class, be sure that you honor the same name casing from your extended class.
you should have configured the $config['subclass_prefix'] on /application/config/config.php. let's say in your case with the value 'N_'
what i can see from your code, you are not extending from CI_Controller and your path seems wrong.
Informative note: the /application/library is used to place classes and libraries from 3rd parties that won't fit into CI schemas.

Accessing other model file within another model file

I am trying to create an class inside the model directory. This class(eg:- Admin) exposes only the methods which makes sense to the controller.
The Admin class will do all the joins and stuffs internally on tables(using ORM) and prepares data which can be readily consumed by the controller.
I have created 15 files in the model directory each of them representing a table in my database using the ORM method.
Now I want to create to create an instance of table within the Admin classes' get_All() method. I have tried to use Kohana::factory() which was unavailable in my Admin class. I tried to create the instance using the 'new', but it ended in an error which says that the specified class is not found.
My class definition for Admin is as follows
<?php defined('SYSPATH') or die('No direct script access.');
class Model_Admin {
public function get_All()
{
$PD = new Model_PayPalData;
echo 'Success';
}
}
The error is:
ErrorException [ Fatal Error ]: Class 'Model_PayPalData' not found
APPPATH/classes/Model/admin.php
Please advice on how to deal with this situation.
Thanks for your attention
Seem like your class is not loaded. You can check this by viewing the declared classes
get_declared_classes();
If it is't there, make sure to include it, or add it to the Models directory, if needed without extending the ORM class.

Codeigniter: how to use multiple 'MY_Model' in my project?

I am new to CI and I am maintaining one project build in CI, previously developed by another guy.
This guy create his own MY_Model in application/core and all models he created extend that MY_Model.
I don't like the core model built by him and want to use Jamie Rumbelow's MY_Model instead in the other models I will create recently.
But since almost all the old models extended the MY_Model already, I can't just replace MY_Model.
How can I use Jamie Rumbelow's MY_Model' and still make old models work?
Thanks for any advices!
You'd have to rename one of the MY_Model classes and put both of them in the MY_Model.php file (or include the renamed one from there). For example, if you were to rename Jamie Rumbelow's class to JR_Model instead, here's how your MY_Model.php file would look like:
class MY_Model extends CI_Model {
// your predecessor's code
}
class JR_Model extends CI_Model {
// Jamie Rumbelow's code
}
... then you can extend from JR_Model wherever you like, without breaking your old models.
Note that it's important to keep the filename as 'MY_Model.php', because that's what CodeIgniter will look for when loading a CI_Model extension.

Categories