简体   繁体   中英

CodeIgniter Undefined property when I call the function model

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM