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();
}
}
Related
I am new in using Codeigniter as web api, I want to get this result
{"result":[{"id":"1","nama":"Orion","nomor":"08576666762"},{"id":"2","nama":"Mars","nomor":"08576666770"},{"id":"7","nama":"Alpha","nomor":"08576666765"}],"success":"1","message":"success"}
but instead I get this kind of result :
{"result":[[{"id":"1","nama":"Orion","nomor":"08576666762"},{"id":"2","nama":"Mars","nomor":"08576666770"},{"id":"7","nama":"Alpha","nomor":"08576666765"}]],"success":"1","message":"success"}
I wonder where am I get it wrong?
I am using codeigniter and my code below is from controller and models
m_server.php (modals)
<?php
Class M_server extends CI_Model {
function __construct(){
parent::__construct();
$this->load->database();
}
// buat view dashboard main
function dash_main1(){
$data = $this->db->query("
select *
from telepon
");
$result = array();
$result['result'] = array();
$result['success'] = "1";
$result['message'] = "success";
array_push($result['result'], $data->result());
return $result;
}
}
Rest_server.php (controller)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Rest_server extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('m_server');
}
public function index()
{
$this->load->helper('url');
$this->load->view('rest_server');
}
function dash_main1(){
$data=$this->m_server->dash_main1();
echo json_encode($data);
}
}
Remove this line (optional)
$result['result'] = array();
And change this line
$result['result'] = $data->result(); //result become the array
array_push add an element to an existing array
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;
}
Model :
class Users_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
public function get_all_users() {
return $this->db->get('users');
}
}
Controller :
class Users extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('security');
$this->load->model('Users_model');
}
public function index() {
redirect('users/view_users');
}
public function view_users() {
$data['query'] = $this->Users_model->get_all_users();
$this->load->view('users/view_all_users', $data);
}
}
My question is where should i put the $this->load->database? In Model or Constructor? If possible tell me why?
And one more question, if i omit the $this->load->database, the error shown
"Undefined property: Users::$db". I'm expecting "Undefined property:
Users_model::$db".
Why is that? Is it looking for $db in both controller or model?
Thank you.
Note: i can connect to database just fine. What im actually ask is if i want to use $this->load->database(). Where should i put it? Controller or Model? And why?
You can either load the database in the controller or you can load it in the model.There's not much of a difference its just more neat and clean that all the interaction in the database is in the model and the controllers the one who connects both views and model.The controller here is like a middleman between the buyer and the seller.
load database in the controller
<?php
class Test_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database('default');
$this->load->model('test_model');
}
public function index()
{
$this->db->from('test');
$query = $this->db->get();
echo "<title>CodeIgniter SQlite</title>";
$data['db']=$query->result();
//print_r($data['db']);
$count = count($data['db']);
echo "Row count: $count rows<br>";
for ($i=0; $i < $count; $i++) {
echo $data['db'][$i]->text." ";
}
}
}
?>
load database in the model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test_model extends CI_Model
{
public $table1 = 'test';
public $table2 = '';
public function __construct()
{
parent::__construct();
$this->load->database('default');
}
public function check_db()
{
$this->db->from($this->table1);
$query = $this->db->get();
return $query->result();
}
}
Go to autoload.php in application/config/autoload.php and add this
$autoload['libraries'] = array('database'); // add database in array(now you dont need to load database at anywhere in project)
Make database connection settings in database.php, file located atapplication/config/database.php
now try this
class Users_model extends CI_Model {
function __construct() {
parent::__construct();
//$this->load->database(); <----remove this
}
public function get_all_users() {
return $this->db->get('users');
}
}
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);
}
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;
}
}