10

你能帮我解决这个问题吗?我是 CodeIgniter 和 PhpStorm 的新手,我遇到了问题。PhpStorm IDE 显示错误,即使代码工作正常(Persons(控制器)类找不到Person(模型)类)。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

$data['persons']=$this->**person**->get_person();= 在Persons类的这种语法上,有一条消息“person在类中找不到字段Persons”。

你能告诉我如何解决这个问题,但实际上输出很好,它可以毫无问题地检索数据,只是在Persons课堂上发出警告消息。

4

3 回答 3

19

person属性 ( $this->property) 没有显式声明,因为它是通过 PHP 的魔术方法创建和访问的。

PhpStorm 对 CodeIgniter 框架没有特别的支持,无法猜测$this->person来自哪里以及它是什么类型。

但是您可以使用@property标记帮助IDE——在实际类之前只是一个小的PHPDoc 注释。

/**
 * @property Person $person Optional description 
 */
class Persons extends CI_Controller {

......哦,神奇 - 它有效: 在此处输入图像描述

于 2013-11-14T18:29:51.787 回答
4

LazyOne 的答案最初对我不起作用。经过一些测试后,我发现我的问题与您如何在 PHPDoc 中声明属性有关 - 希望以下观察可以帮助其他人。这就是我必须声明我的模型类的内容:

class Custom extends CI_Model {}

在我的控制器中,我通过以下方式加载和使用模型:

$this->load->model('Custom')
$table = $this->Custom->get();

现在为了让 phpStorm 正确选择这个类,我最初在核心类上方添加了一个 PHPDoc @property 注释,正如其他人所描述的那样(在 CI_Controller 类或单独的 CI_phpStrom.php 文件上方),如下所示:

*
* @property Custom $custom
*

但是,这并没有解决问题,因为在这种情况下变量名区分大小写,我不得不写:

*
* @property Custom $Custom
*

让我上面的控制器代码正确地选择类。另一种方法是在调用函数时使用小写字母(即使您的模型声明使用大写字母也有效)

$this->load->model('custom')
$table = $this->custom->get();

如果我将模型类称为“Custom_model”,那么所有这些大写或小写的有趣事情都无关紧要 - 如果将 PHPDoc 属性变量设置为 $Custom_model 或 $custom_model ,它就不会改变......

于 2014-06-11T02:44:03.893 回答
-1

Normally most people add the ‘<code>_model’ suffix to the Model class names. I suggest that you rename your model with "Person_model" call your model "person_model" like this:

$this->load->model('person_model');
$this->person_model->get_person();

I think that you might find it an adequate solution!

于 2013-11-14T17:49:33.417 回答