PHP CodeIgniter models with the same name - php

I need your help, please!
I have 2 models with the same name
1.... application/admin/user/models/User_model.php, with this code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model
?>
2.... application/front/user/models/User_model.php, with this code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model
?>
Both classes without differences.
I need to use application/admin/user/models/User_model.php
from application/front/user/controllers/User.php
I tried to do the following changes:
If I add in application/front/user/models/User_model.php the namespace like this code:
<?php namespace customers;
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends \CI_Model
<?
And from application/front/user/controllers/User.php, I call application/admin/user/models/User_model.php always gives me an error as application/front/user/models/User_model.php was not loaded.
If I delete the namespace from application/front/user/models/User_model.php my code always goes to this mode NEVER to admin/user/user_model.
I need to AVOID change admin/user/User_model.php because a lot of classes uses this MODEL.I only can change front/user/User_model.php
But I need to USE admin/user/user_model.
I call application/admin/user/models/User_model.php like this:
$this->load->model('user_model');
I need to load application/admin/user/models/User_model.php
Please help me!!!!
Thx!
Ani

That is simply not possible, since Codeigniter doesn't support Namespaces
Usually you would see an error like
Fatal error: Cannot declare class `User_model`, because the name is already in use
But Codeigniter prevents this, because it doesn't load any models which are already loaded. You can see this here.
So the only way out of your misery is to simply rename your models - even if you don't want to ;)

Hope this helps you
You can created subdirectory in models like this :
application/models/admin/User_model.php
application/models/user/User_model2.php
Load the model like this:
$this->load->model('admin/User_model');
$this->load->model('user/User_model2');
you should assigned your model to a different object name you can specify it via the second parameter of the loading method:
Use them like this :
$this->load->model('admin/User_model', 'admin_model');
$this->admin_model->some_method();
$this->load->model('user/User_model2', 'user_model');
$this->user_model->some_method();
For more : https://codeigniter.com/user_guide/general/models.html

Related

Namespace not found on extending class

I have a problem with namespaces. Follow the code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class NavBar extends fwportal\controllers\template\NavBar {
function __construct()
{
var_dump('navBarPortal');
parent::__construct();
}
}
And the main class:
<?php
namespace fwportal\controllers\template;
use fwportal\controllers\NavbarPermissoes;
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
Abstract class NavBar extends \CI_Controller
{}
This return the error below:
Fatal error: Class 'fwportal\controllers\template\NavBar' not found in /var/www/portalsibe/sistema/controllers/template/NavBar.php on line 6
Anyone can help me with this?
I don't know why this error occur, because I used in other files with the same mode and works ok.
If you are using Codeigniter 3 then most probably you can not extend "\CI_Controller" while you are defining namespace on a class.
May be this is the reason of getting error.

wiredesignz modular extensions extending controller

I am using WireDesignz modular extensions with great success so far. I now have the need to extend a controller within the module. I have created the new controller and the original, now extended, controller and they work fine outside of the HMVC but when I put them inside the module folder and call the new controller it days is can't find the controller it's extending... even though it's right there in the same directory. If I call the original one it's all fine. I'm not sure where to go with this as I can't find anyone with the same problem online. Any ideas? Here's a bit more:
Original controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar extends MY_Controller {...
New controller, in same directory:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar_new extends Calendar {...
Results in:
Fatal error: Class 'Calendar' not found in /home/d/e/demo/web/public_html/application/modules/calendar/controllers/calendar_new.php on line 2
Thanks.
The base controller class which you are extending is not being included as a resource. Codeigniter will not automatically try to load base classes.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include('Calendar.php');
class Calendar_new extends Calendar {...

Codeigniter error log messages

I have been using codeigniter for sometime now. But I am still new to it. And when I bump into some problem, its takes days to figure it out and solve it.
Unable to locate the model you have specified: page_m
I usullay face these sort of errors too. However since I am using multiple hierarchy of classes (Controllers) I am not being able to figure it out.
Is there any way I could make codeigniter to log the error messages with the line number??
You have to create model in Application/Models directory. and make sure you extend your CI_Model class.
eg. model name : my_model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class my_model extends CI_Model {
public function __construct() {
$this->load->database();
}
}
Are you sure that the file name is the same as the name of the class?
If your file is page_model.php the class must start with class Page_Model extends CI_Model
And in your controller remember $this->load->model('page_model');

Extended core class not found

I have some codeigniter application. It works well on my local server. After I uploaded it to the hosting server it won't work and resulting an error:
Fatal error: Class 'System_Controller' not found in /home/k2113138/public_html/test/application/controllers/login.php on line 3
Here is my System Controller that extending CI_Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//location: application/core/
class System_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
//some code
}
}
my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//location: application/controllers/
class Login extends System_Controller {
public function __construct()
{
parent::__construct();
}
}
i have set my config as my desired configuration like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = 'http://xx.com/subfolder/';
$config['subclass_prefix'] = 'System_';
?>
I have read many article that discuss this matter but still cannot found the solution. Really confuse about the problem, because everything works well in local server. I have configured the config file as what it's needed like the database and the routes configuration. Is there any other things that i need to re-configure?
EDIT: My Codeigniter version is 2.1.3
Ok, you changed the filename to lowercase, now, try change the classname too:
class system_controller extends CI_Controller
If don't run, play with the classname and the filename, this is a problem of your server, very common in shared servers.
Sometimes, the PHP version and its configuration, play a important rol in this cases.

codeigniter -> having trouble loading multiple libraries/classes

Ok, so in my base controller (page.php) I have the following code which works fine:
$this->load->library('Siteclass');
$mysite = new site_model();
The siteclass library references a model named site_model and instantiates based on data received from that model. All is good.
Now I want to load another library so that I can instantiate another object as well. So I add this to page.php:
$this->load->library('Memberclass');
$mysite = new member_model();
But now I get the following error:
Message: Undefined property: Memberclass::$site_model
Filename: libraries/Loader.php
Line Number: 1035
From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. I've checked my code and I am definitely calling the correct files.
Here's what Siteclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Siteclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->load_site_data();
// etc etc
and here's what Memberclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
// etc etc
Thanks in advance for any help!
Gary
I think you're confused about how MVC works in CodeIgniter. Why are you using the loader class to create a controller? Why are you creating a stand-alone instance of your model outside of your controller class?
In CodeIgniter, your URLs represent paths to your controllers' methods. That means that your "base controller" should automatically be instantiated if you go to:
www.example.com/memberclass
Or perhaps more to the point, if you have a link like this:
www.example.com/page
You should have a file in your /application/controllers directory called page.php which looks like this:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Controller {
function __construct() {
parent::Controller();
// etc etc
Furthermore, unless you're loading data from your model to be used every single time you call this controller, you'll want to put your model calls inside a non-constructor method of this class. Something like:
class Page extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
$this->load->view('myview', array('data'=>$data));
}
}
So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword.

Categories