[英]CodeIgniter Undefined property when I call the function model
please I need help, I'm looking for the mistake from 1 day ago 请我需要帮助,我正在寻找1天前的错误
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 . 这是我的模型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 控制器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. 在我的autoload.php文件中,我已经加载了“数据库”库。
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. 我必须创建对象,因为导入模型codeigniter不能创建实例对象。
$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'); usuario_model.php和$ 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. 从控制器U调用加载模型时使用大写字母,但实际模型文件名使用小写字母。 So, model file is missing/not found. 因此,模型文件丢失/找不到。
are you getting $username value in controller? 您在控制器中获得$ username值吗? this link should help read the question part. 该链接应有助于阅读问题部分。 include function used before CodeIgniter class changes the view loading order CodeIgniter类更改视图加载顺序之前使用的include函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.