简体   繁体   中英

Undefined property when calling function from model

When I try and load a Model, I get no issues. The code is as follows (in controller):

applications/controllers/shortlinks.php :

public function __construct() {
    parent::__construct();
    $this->load->library("logged");
    $this->load->model("shortlinks_logic"); //This model I'm interested in
}

As example, the shortlinks_logic model has this code in:

applications/models/shortlinks_logic.php

class Shortlinks_logic extends CI_Model {

    public function test() {
        echo "TEST";
    }

}

No issues there either. However, when I try and call this function in the controller:

public function something($argument_one, $argument_two) {
    $this->shortlinks_logic->test(); //Line 35 of following error
}

I get this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Shortlinks::$shortlinks_logic

Filename: controllers/shortlinks.php

Line Number: 35 Fatal error: Call to a member function test() on a non-object in C:\\xampp\\htdocs\\tools_v2\\application\\controllers\\shortlinks.php on line 35

I scouted StackOverflow, eventually coming to this answer , but I realized I don't want to this for every function of my controller and I shouldn't have to add

$logic = new shortlinks_logic();

and call everything by

$logic->function();

because I know I can give the model an alias on load anyway

$this->load->model("shortlinks_logic", "logic");

and use

$this->logic->test();

Is there a reason why I need to create a new object in each function and is there a way to fix this?

I know I can autoload all the models which is an option, but I wanted to avoid this if possible, as not everyone will have access to all models and there are quite a few models, so I want certain models to go to certain users (hence why I want to load inside the controller).

I understand this is quite lengthy (apologies) so I bolded question.

Tested your code and working perfectly :- created filname shortlinks_logic.php of model file under application/models

class Shortlinks_logic extends CI_Model {
    public function test() {
        echo "TEST";
    }
}

my controller shortlinks.php

<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
class Shortlinks extends CI_Controller {
  public function __construct() {
    parent::__construct();
    $this->load->model("shortlinks_logic"); 
  }
  public function something() {
    $this->shortlinks_logic->test(); 
  }
}

and you will get working perfectly.

maybe your model not load properly...

try this:

public function __construct() {

    $this->load->library("logged");
    $this->load->model("shortlinks_logic");
    parent::__construct();
}

I figured out what the problem was to this.

For some reason, it wasn't allowing me to load

$this->load->library("logged");

before

$this->load->model("shortlinks_logic");

I had to load the model first.

eg:

$this->load->model("shortlinks_logic");
$this->load->library("logged");

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