简体   繁体   English

从模型调用函数时属性未定义

[英]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 : 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: 例如, shortlinks_logic模型在以下代码中包含以下代码:

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 遇到PHP错误

Severity: Notice 严重程度:注意

Message: Undefined property: Shortlinks::$shortlinks_logic 消息:未定义的属性:Shortlinks :: $ shortlinks_logic

Filename: controllers/shortlinks.php 文件名: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 行号:35致命错误:在第35行的C:\\ xampp \\ htdocs \\ tools_v2 \\ application \\ controllers \\ shortlinks.php中的非对象上调用成员函数test()

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 我对StackOverflow进行了筛选,最终得到了这个答案 ,但是我意识到我不想为我的控制器的每个功能都这样做,因此我不必添加

$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 测试了您的代码并完美运行:-在application/models下创建了模型文件的filname shortlinks_logic.php

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

my controller shortlinks.php 我的控制器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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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