Codeigniter on Ubuntu not Loading Model - php

I've recently switched from Windows 7 to Ubuntu.. Now I'm having this strange problem with codeigniter.. My code works prefect on Widnows 7 Xampp server, but when I try to access it on ubuntu having apache2, I cannot load any model, libraries etc.
Here is my code for model
<?php
class Usermodel extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->user_per_page = 8;
}
function getUser($id)
{
$query = $this->db->query("SELECT * FROM user WHERE id = $id");
if(intval($query->num_rows()) > 0)
{
$data = $query->result();
return $data[0];
}
else
return null;
}
}
Here is the code of my controller where i am loading model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
public function index()
{
$this->load->model("Usermodel");
$fb_config = array(
'appId' => 'xxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx'
);
$this->load->library('facebook', $fb_config);
$this->load->helper('security');
}
}
I've already tried the following
1. Changed usermodel.php to Usermodel.php
2. Changed $this-load->model("Usermodel") to $this-load->model("usermodel")
but none seem to work
I get this fatal error when i call $this->Usermodel->getUser(1) in index() function of my controller
PHP Fatal error: Call to a member function getUser() on a non-object in /var/www/voicebuds/application/controllers/register.php on line 19, referer: mysite
UPDATE
If i put the Usermodel in config/autoload.php, it works fine.. So I must say there is some problem with loader function.

check your model file name for model
<?php
// file name usermodel.php
class Usermodel extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->user_per_page = 8;
}
function getUser($id)
{
$query = $this->db->query("SELECT * FROM user WHERE id = $id");
if(intval($query->num_rows()) > 0)
{
$data = $query->result();
return $data[0];
}
else
return null;
}
}
in your controller load appropriate model using model file name
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
public function index()
{
$this->load->model("usermodel");//your file name of model
$fb_config = array(
'appId' => '454005957970086',
'secret' => '7b8e9664cb0439f88da00007996278c7'
);
$this->load->library('facebook', $fb_config);
$this->load->helper('security');
}
}

Model classes are stored in your
application/models/
folder. They can be nested within sub-folders if you want this type of organization.
The basic prototype for a model class is this:
class Model_name extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.
The file name will be a lower case version of your class name. For example, if your class is this:
class User_model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Your file will be this:
application/models/user_model.php
So please check, is your model in any sub-folder or not.
It is said to be a best practice to load models, libraries in __construct (constructor)
class User extends CI_Controller
{
public function __construct()
{
parent:: __construct();
$this->load->model('usermodel');
}
}
I found in your controller there is not construct. try to add constructor and load usermodel there

I've ended up using __construct() in controller class.. I now first initialize model in constructor and then call in in function and its working now.!! But still I'm puzzled why load-model() didn't work for me in index() function
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->model("Usermodel");
$fb_config = array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
);
$this->load->library('facebook', $fb_config);
$this->load->helper('security');
}
public function index()
{
$data = $this->Usermodel->getUser(1);
print_r($data);die;
}
}

Related

Passing parameters to __construct library in Codeigniter

I am really new to Codeigniter, and just learning from scratch. checked the documentation on Creating Libraries but no success on my example:
I need to pass a value to __construct library.
class: libraries/Myclasses/Bird
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Bird{
public $fly;
public $goodsound;
public function __construct($fly, $goodsound) {
$fly = $this->fly;
$goodsound = $this->goodsound;
}
public function sentance(){
return "This Bird can ".$this->fly . " and has ". $this->goodsound;
}
}
class: libraries/Mybird
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'libraries/Myclasses/Bird.php');
class Mybird extends Bird {
public function __construct() {
}
}
controller: Birds
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Birds extends CI_Controller {
public function __construct(){
parent::__construct();
$config = array('fly' => 'fly', 'goodsound' => 'very good');
$this->load->library('Mybird', $config);
}
public function index(){
$mybird = new mybird();
echo $mybird->sentance();
}
}
I think that the problem is in Mybird class that not passing the values but i can't figure out how to handle it.
One issue is in the constructor for Bird. Try this.
class Bird{
public $fly;
public $goodsound;
public function __construct($fly, $goodsound)
{
$this->fly = $fly;
$this->goodsound = $goodsound;
}
Without the $this-> before the property name you are creating local variables that will go out of scope when the constructor ends.
Secondly, any class extending Bird should pass two arguments to the base class constructor. For instance:
class Mybird extends Bird {
public function __construct()
{
parent::__construct('Fly', 'very good');
}
}
You could define Mybird to accept arguments too and then pass those to parent::__construct
class Mybird extends Bird {
public function __construct($fly, $goodsound)
{
parent::__construct($fly, $goodsound);
}
}
There is no need for the new call in the Controller - $this->load->library('Mybird', $config); did that for you already.
index() should work fine as shown below. Note that Mybird is a property of the controller and so needs to be accessed using $this.
public function index(){
echo $this->Mybird->sentance();
}
However, if you want to pass a $config array as an argument when loading a library then you need to revise both the Bird and the Mybird classes like so.
class Bird
{
public $fly;
public $goodsound;
public function __construct($config)
{
$this->fly = $config['fly'];
$this->goodsound = $config['goodsound'];
}
}
class Mybird extends Bird
{
public function __construct($config)
{
parent::__construct($config);
}
}
Your library Mybird also needs to be set to except parameters in its constructor when the library is called from the controller Bird with $this->load->library('Mybird', $config);
when extending the library, you need to stick to what is set in your config.php, something like $config['subclass_prefix'] = 'MY_'; would require MY_Bird instead of Mybird
more info on the subject here and here

Load a model in Codeigniter 3.0.3

I have a project whith this structure in controllers folder:
places.php
users.php
items.php
...
And in my models folder:
Place.php (the name inside is class Place extends ActiveRecord\Model)
User.php
...
Inside my controller places.php if I want to load a model I have to do just that:
$this->load->model('Place');
And after that I have to call my method like that:
$this->place->get_all_places();
It was work in my localhost but not in my server I check my php version in the server and it's 5.6.
How I can fix that?
That's my model file Place.php
class Place extends ActiveRecord\Model
{
static $table_name = 'places';
static $has_many = array(
);
public function get_all_places()
{
return true;
}
}
That's my controller file places.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Places extends MY_Controller {
function __construct()
{
parent::__construct();
if($this->user){
$access = TRUE;
}else{
redirect('login');
}
}
function index()
{
$this->content_view = 'places/all';
}
function get_places(){
$this->theme_view = '';
$this->load->model('Place');
$this->place->get_all_places();
}
}
And the error it was that:
Unable to locate the model you have specified: place
Your model file name should be
Places_model.php
and inside model
class Places_Model extends CI_Model # Changed
{
static $table_name = 'places';
static $has_many = array();
public function get_all_places()
{
return true;
}
}
Models in Codeigniter
I think every thing is right.But the problem is due to this line..
class Place extends ActiveRecord\Model
If you define a new model which extends the CI_Model.And then name of new model can not like as ActiveRecord\Model.Name may be as ActiveRecordModel.SO one solution for you can be..replace above line as below if you define the new model named as ActiveRecordModel in application/core folder.
class Place extends ActiveRecordModel
Another solution can be if you have not define any new model.then just replace above line as
class Place extends CI_Model

Can I load and use a model in MY_Controller using CI3

I'm using Codeigniter 3 and I need some data available to all methods. I will query the data from the database and then I need to display it on every page.
I have created a MY_Controller extending CI_Controller and saved it in /application/core/
But I am getting this error:
Fatal error: Call to undefined method Area_model::get_user_locations()
MY_Controller:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
$this->location_data = $this->area_model->get_user_locations($org_id);
}
}
Can i access models and the database from within MY_controller?
Area_model.php
// get all areas for an organisation
public function get_user_locations($org_id) {
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}
MY_Controller
not used to direct any function to __construct()
__construct to only load to any model
write other function to MY_Controller look like this
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
}
}
function functionname()
{
$this->location_data = $this->area_model->get_user_locations($org_id);
}
Area_model.php
public function get_user_locations($org_id)
{
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}

CodeIgniter, header controller is not loading index method

Firstly I am getting no errors, I am trying to create an is_logged_in() method in my header model in Code Igniter, but nothing in the index method of the controller will load. I added die(); into it and even that wont execute, Here is my code:
header.php - controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Header extends CI_Controller {
public function index() {
print_r($this->session->all_userdata());
$data = array();
$data['title'] = 'Wenso - Timesheet';
$username = $this->session->userdata('username');
$this->load->view('template/header', $data);
$this->load->model('header_model');
$is_logged_in = $this->header_model->is_logged_in($username);
die($is_logged_in);
}
}
header_model.php - Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Header_model extends CI_Model {
public function is_logged_in($username){
$q = $this
->db
->where('email_address', $username)
->limit(1)
->get('users');
die($q->last_query());
if($q->row('username') != $username){
return FALSE;
} else {
return TRUE;
}
}
}
Note: none of the die() functions in my code work.. Anything I add into the index function of the controller (which to my understanding is loaded by default) does not get executed...
Thanks in advance
public function __construct() { parent::__construct(); }
Add this method at your model else you wont have $this->db loaded
As AdrienXL pointed out the controller is only loaded whern the url /controller_name is called.. This wasn;t the case in my user case scenario.
Also something worth pointing out as Sevtilo mentioned above if you create a construct method in CodeIgniter you ovewrite the dafult calls for things such as $this->db class, using:
public function __construct() {
parent::__contsruct();
}
Will get the parent classes contsructor.
Regards
Ric
If you want to call this code transparently (ie without having to put any extra mess in the uri) then move the code into the constructor of an extension called MY_Controller.php in application/core that looks a bit like this...
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
print_r($this->session->all_userdata());
$data = array(); $data['title'] = 'Wenso - Timesheet';
$username = $this->session->userdata('username');
$this->load->view('template/header', $data);
$this->load->model('header_model');
$is_logged_in = $this->header_model->is_logged_in($username);
die($is_logged_in);
}
}
And then in your application/controllers files extend this class like
class Some_controller extends MY_Controller{
function __construct (){
parent::__construct();
}
public function index(){
//your header code will be run before this or any other method in this class
}
}
And the code from MY_Controller.php will run before any of your methods.

What is wrong with my Model and Controller?

For the life of me I cannot understand why I am receiving errors with my install of CodeIgniter and this current MVC set up.
The error I see is
Fatal error: Call to undefined method Login::users_model() in /var/www/application/controllers/login.php on line 17
Controller application/controllers/login.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model()->test_user();
$this->load->view('home',$data);
}
}
Model application/models/users_model.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function test_user() {
return 'Test User #1';
}
}
View application/views/home.php
echo $testing;
No need for function bracket with model name
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model->test_user();
$this->load->view('home',$data);
}
}
Replace $data['testing'] = $this->users_model()->test_user(); with $data['testing'] = $this->users_model->test_user(); in your controller.
Simply load your model inside the constructor
public function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
Then simply call the relevant functions inside any controller functions like this.
public function index()
{
$data = '';
$data['testing'] = $this-> users_model-> test_user();
$this->load->view('home',$data);
}

Categories