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);
}
Related
I am new in codeigniter, I've read how to load some models with this framework. Now i have a problem regarding with using my model. I have an error of Message: Call to undefined method User_model::select().
My situation is simple, I want to implement a data table and i am using my model by it. Here is what my error comes:
//total records
foreach ($this->user->select("COUNT(*) AS count", $where, $join) as $key => $value) {
$data["iTotalDisplayRecords"] = $value->count;
$data["iTotalRecords"] = $value->count;
}
as you can see, i was using $this->user which is refers to my model i loaded in my constructor:
public function __construct() {
parent::__construct();
if (!$this->ion_auth->logged_in()){
redirect('auth/login', 'refresh');//redirect them to the login page
}
if (!$this->ion_auth->is_admin()){
redirect(base_url().'login', 'refresh');
}else{
$this->admin_id = $this->session->userdata('user_id');
$this->load->library("pagination");
$this->load->model('user_model','user');
}
}
Here is the code of my model:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct() {
if ($this->ion_auth->logged_in()){
$this->user_id = $this->session->userdata('user_id');
$group = $this->ion_auth->get_users_groups($this->user_id)->result();
$this->group_id = $group[0]->id;
}
}
public $tbl_name = "users";
}
No this is not the way to call select in CI3. You have to write your own method in the model class then call that method.
I have rewrite your model, try this:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class User_model extends CI_Model
{
private $tbl_name = "users";
public function __construct()
{
if ($this->ion_auth->logged_in()) {
$this->user_id = $this->session->userdata('user_id');
$group = $this->ion_auth->get_users_groups($this->user_id)->result();
$this->group_id = $group[0]->id;
}
}
public function select($select, $where, $join)
{
$this->db->select($select);
$this->db->from($this->$tbl_name);
$this->db->where($where);
$this->db->join($join);
return $this->db->get()->result();
}
}
I am creating a model there is no error in my case, but it give me error
Message: Call to undefined method Auth_model::chech_user()
Here is my code
Controller
class Auth extends CI_Controller
{
public function __construct()
{
parent::__construct();
$params = array(
'iteration_count_log2' => '8',
'portable_hashes' => TRUE
);
$this->load->library('hash', $params);
$this->load->model('auth_model');
}
public function login()
{
if (isset($_POST) && !empty($_POST)) {
print_r($this->auth_model->chech_user($_POST));
exit;
}
$this->load->view('auth/login');
$this->load->view('layouts/footer');
}
Model
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auth_model extends CI_Model
{
public function check_user($params = array())
{
echo 'done';
}
}
There's a typo within the code you've provided, you're trying to call chech_user() not check_user() within the controller.
change this line
print_r($this->auth_model->chech_user($_POST));
to
print_r($this->auth_model->check_user($_POST));
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;
}
This is not the only controller in the folder. I already deleted index.php.
but once i add this part of a function for a shopping cart
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller
{
public function index($index = 0)
{
$data['listProduct'] = $this->mproduct->findAll();
$this->load->view('template/header.php', $data);
$this->load->view('index', $data);
}
}
it gives me this error
Message: Undefined property: Product::$mproduct
Filename: controllers/product.php
Line Number: 7
here is my model
<?php
class MProduct extends CI_Model
{
function _construct()
{
parent::_construct();
}
function findAll()
{
return $this->db->get('product')->result();
}
function find($id)
{
$this->db->where('id', $id);
return $this->db->get('product')->row();
}
}
You need to load the model first before calling it.
Also the first letter should be capitalized with rest of the name should be in lowercase.
Class names must have the first letter capitalized with the rest of
the name lowercase. Make sure your class extends the base Model class.
Refer
Add this:
$this->load->model ( 'Mproduct' );
$data['listProduct'] = $this->Mproduct->findAll();
Full code :
Controller : controllers/Product.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller {
public function index()
{
$this->load->model ( 'Mproduct' );
$data['listProduct'] = $this->Mproduct->findAll();
$this->load->view('template/header.php', $data);
$this->load->view('index', $data);
}
}
Model : models/Mproduct.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mproduct extends CI_Model {
function _construct()
{
parent::_construct();
}
function findAll()
{
echo 'model';
}
function find($id)
{
echo 'model';
}
}
I have this controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('text');
}
public function index()
{
$this->home();
}
public function home()
{
$data['title']="Somesite";
$this->load->view("view_home", $data);
}
public function blog()
{
$data['title']="Somesite";
$this->load->view("view_blog", $data);
}
public function answers()
{
$data['title']="Somesite";
$this->load->view("view_answers", $data);
}
}
As you may see $data['title'] is same for all functions, how to make it simpler, to include at the beggining and not to write it in every function, repeat again, and then transfer to view.
Is there a way to tranfer it to function?
Before construct function add this:
public $data = array();
Then in the construct function write:
$this->data['title']="Somesite";
And finally before load view add this:
$data = $this->data + $data;
Now you have same $title everywhere.
Here si simple solution and elegant for transfering one variable to all views :)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
//Class-wide variable to store stats line
protected $title;
function __construct()
{
parent::__construct();
$data->title = "Some site";
$this->load->vars($data);
}
I'm using this method in every projects.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
//Global variable
public $outputData = array();
public $loggedInUser;
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->helper('url');
$this->load->view('users/users');
}
public function register() {
parent::__construct();
$this->load->helper('url');
$this->load->model('users_model');
//get country names
$countryList = $this->users_model->getCountries();
$this->outputData['countryList'] = $countryList;
$this->outputData['pageTitle'] = "User Registration";
$this->load->view('users/register',$this->outputData);
}
}
View file
<?php if(isset($pageTitle)) echo $pageTitle; ?>
<?php
if(isset($countryList)){
print_r($countryList);
}
?>