CodeIgniter Undefined property when I call the function model - php

please I need help, I'm looking for the mistake from 1 day ago
Severity: Notice
Message: Undefined property: Login::$Usuario_model
Filename: api/login.php
Line Number: 27
Fatal error: Call to a member function obtener_usuario() on a non-object in /home/fpincheira/web/cobranza/application/controllers/api/login.php on line 27
this is my model usuario_model.php. This query is a example, my real query is other
<?php
class Usuario_model extends CI_Model {
function __construct(){
parent::__construct();
$this->db1 = $this->load->database('db1', TRUE);
}
function obtener_usuario($rut){
$sql = "SELECT sysdate FROM dual"; //this is a example query
$query = $this->db1->query($sql, array($rut));
return $query->result_array();
}
}
?>
The controller login.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/REST_Controller.php';
class Login extends REST_Controller {
function __construct(){
parent::__construct();
}
function validarUsuario_post(){
try{
$username = $this->post('user');
$this->load->model('Usuario_model','usuario');
$perfil_usuario = $this->usuario->obtener_usuario($username);
//print_r($perfil_usuario);
}catch(Exception $e){
$this->response(array('error'=>$e->getMessage()), $e->getCode());
}
}
}
?>
In my autoload.php I have loaded 'database' library.
Can you see the mistake?

You need replace the following lines :
$this->load->model('Usuario_model','usuario');
$perfil_usuario = $this->usuario->obtener_usuario($username);
with :
$this->load->model('usuario_model');
$perfil_usuario = $this->usuario_model->obtener_usuario($username);
Try and let me know by commenting.

answer the question.
I had to create the object, because to import the model codeigniter not create the instance the object.
$this->load->model('usuario_model');
$usuario = new usuario_model();
$perfil_usuario = $usuario->obtener_usuario($username);
this way I could access to function of model.
If somebody have a best answer, pliss comment, thanks
Regards

usuario_model.php and $this->load->model('Usuario_model','usuario'); Don't you think lowercase and uppercase model name is making difference. While calling to load model from controller U is in caps but actual model filename is in lowercase. So, model file is missing/not found.
are you getting $username value in controller? this link should help read the question part. include function used before CodeIgniter class changes the view loading order

Related

Error : Object of class CI_DB_mysql_result could not be converted to string | already searching

I'm new to CodeIgniter, I've tried to read the documentation and search to on stackoverflow of CI but I still can't solve my problem, maybe someone here can help fix my problem. Here is my code and error:
in my controller
$data['pembayaranpemesanan'] = $this->global_model->getSelectedData('tbl_pemesanan', $data);
in my model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Global_model extends CI_Model
{
public function getSelectedData($table,$data)
{
return $this->db->get_where($table, $data);
}
}
You have to use row() or result() in the function to get record form DB
class Global_model extends CI_Model
{
public function getSelectedData($table,$data)
{
//To get more than one row
return $this->db->get_where($table, $data)->result();
//To get only one row
//return $this->db->get_where($table, $data)->row();
}
}
For more information read this doc

How to fix Codeigniter error: Call to a member function on null?

It's my first time exploring codeigniter and I've been trying to create a login where if users are not found in the database, it will redirect back to the login screen. I have not been successful because I've been getting this error:
first error message
Followed by
second error message
I've tried loading the database and session inside function verify() using
$this->load->database();
$this->load->session();
but even so, I didn't think it would work since I already declared database and session in autoload.php
$autoload['libraries'] = array('database', 'session');
Here's the controllers/Login.php
class Login extends CI_Controller{
function index(){
$this->load->view("templates/_header");
$this->load->view("login");
$this->load->view("templates/_footer");
$this->load->view("templates/_scripts");
}
function verify(){
$this->load->model('user');
$check = $this->model->validate();
if($check){
} else {
redirect('login');
}
}
}
Here's the models/User.php
<?php
class User extends CI_Model{
function validate(){
$arr['username'] = $this->input->post('username');
$arr['password'] = password_hash($this->input->post('password', PASSWORD_DEFAULT));
return $this->db->get_where('users', $arr)->row();
}
}
What do you guys think is the problem with it?
you have load your model true way but to use it you have to call its class name (here is User) to work work correctly.
note: if you want to code in a clean way and don't conflict them try to name your models like User_model or User_m, so if you have a controller with name of User its not conflict with your model.
:-)

Codeigniter call to Member function Error

Hi all I am having an error and I just can't see why codeigniter is throwing the error:
controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management extends CI_Controller {
public function index(){
//calls login page view
$this->managementView();
}
public function managementView(){
//loads course page view
$users['users'] = $this->management_model->getInfo();
$this->load->view("header");
$this->load->view("users", $users);
$this->load->view("footer");
}
}
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management_model extends CI_Model{
function getInfo(){
$query = $this->db->get("users");
$result = $query->result_array();
return $result;
}
}
I am receiving the following error, I just can't seem to see what the hell I'm doing wrong - I'm new to web stuff so don't know what these errors indicate:
Fatal error: Call to a member function getInfo() on a non-object
also see this:
Severity: Notice
Message: Undefined property: Management::$management_model
Filename: controllers/management.php
Line Number: 12
I can't see the issue? - If anyone could point it out would be much appreciated.
you need to load the model before calling any function from the model.
for eg:
$this->load->model('management_model');
$users['users'] = $this->management_model->getInfo();
or you can load it via the constructor.
function __construct() {
parent::__construct();
$this->load->model('management_model');
}
}
the notice you are getting is clearly telling about the undefined property management model
you can see more about this here
You dont appear to ever set the management_model property in your controller.
I would expect to see something like this somewhere in your controller:
$this->management_model = new Management_model();
Initially you can load the model like this
$this->load->model('management_model');
then only u can use the object to call function
you need some more clarity Click
use
$this->load->model('management_model');
before
$users['users'] = $this->management_model->getInfo();

Controller called inside another controller, after that DB calls wont work - Codeigniter

I have a controller, which calls another controller, and after that a DB call happens, now when i run the DB call it doesn't work, the error i get is
<p>Message: Undefined property: Advertisement::$admodelobj</p>
<p>Filename: v1/Advertisement.php</p>
This is how my controller looks
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Advertisement extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function getads()
{
//another controller call
require_once 'application/controllers/v1/ip2locale.php';
$GetLocale = new ip2locale();
$range = $GetLocale->index($clientip);
//now the db call
$this->load->model('ads_model','admodelobj');
$campaigns = $this->admodelobj->getCampaigns('desktop',1.00,'IN');
}
}
Now if i just put the db call above the "Another controller call" it works good, but right after the "another controller call" it gives me the error, what could be the issue?
Fixed it, one has to load the controller back again to avoid the confusion, and it starts working :)
$ci =& get_instance();
$ci->load->model('ads_model','admodelobj');
$campaigns = $ci->admodelobj->getCampaigns($deviceTypeValue,$payout,$locale);
Great Brij Raj Singh... Just tell, I have one way :
public function getads(){
$this->load->library('../controllers/ip2locale');
$range = $this->ip2locale->index($clientip);
.....
}
And it would works :)

How do I access my Models in CodeIgniter?

I'm trying to get familiar with CI and I have run into a problem while trying to implement my Model. I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$OrderModel
Filename: controllers/home.php
Line Number: 12
My assumption was that I was breaking some convention with the naming of the Model. If
I change the line in question and call the model using all lower case:
$data['query'] = $this->ordermodel->get_all_workorder_names();
Nothing is returned to the view.. blank page; no source, no error.
Here is my model:
<?php
class OrderModel extends Model{
function OrderModel()
{
parent::Model();
$db = $this->load->database();
}
function get_all_workorder_names()
{
$this->db->select('name');
$query = $this->db->get('WorkOrder');
return $query->result();
}
}
?>
This is the calling controller:
<?php
class Home extends Controller{
function Home()
{
parent::Controller();
$this->load->model('ordermodel');
$this->load->helper('url');
}
function index()
{
$data['query'] = $this->OrderModel->get_all_workorder_names();
$this->load->view('Header');
//$this->load->view('Home',$data);
$this->load->view('Footer');
}
}
?>
What am I doing wrong? A side question: Is there a debugger available for PHP in Eclipse?
Use $this->ordermodel. The variable name will match whatever you use with load->model()
For further reading, you can check out CodeIgniter's User Guide entry on loading models here.

Categories