简体   繁体   English

找不到PhpStorm CodeIgniter类

[英]PhpStorm CodeIgniter class not found

Can you help me on this one. 你能帮我解决这个问题。 I'm new in CodeIgniter and PhpStorm, I'm having a problem. 我是CodeIgniter和PhpStorm的新手,我遇到了问题。 The PhpStorm IDE is showing an error, even though the code works fine (the Persons (controller) class can't find the Person (model) class). PhpStorm IDE显示错误,即使代码工作正常( Persons (控制器)类无法找到Person (模型)类)。

在此输入图像描述

在此输入图像描述

在此输入图像描述

$data['persons']=$this->**person**->get_person(); = on this syntax from Persons class, there is a message "Field person not found in class Persons ". =来自Persons类的这个语法,有一条消息“在类Persons找不到Field person ”。

Can you enlighten me on how to resolve this, but actually the output is good and it retrieves the data without issue just a warning message in Persons class. 你可以告诉我如何解决这个问题,但实际上输出是好的,它会在Persons类中检测数据而不会发出警告消息。

The person property ( $this->property ) is not explicitly declared as it is created and accessed via PHP's magic methods. person属性( $this->property )未显式声明,因为它是通过PHP的魔术方法创建和访问的。

PhpStorm has no special support for CodeIgniter framework and cannot guess where $this->person is coming from and what type it is. PhpStorm没有对CodeIgniter框架的特殊支持,也无法猜测$this->person来自哪里以及它是什么类型。

But you can help IDE -- just a small PHPDoc comment before the actual class -- using @property tag. 但您可以使用@property标记帮助IDE - 在实际类之前只是一个小的PHPDoc注释。

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

...and oh magic -- it works: ......而且魔术 - 它有效: 在此输入图像描述

The answer from LazyOne did not work for me initially. LazyOne的答案最初对我不起作用。 After some testing I found out that my issue was upper/lower case related on how you declare the property in PHPDoc - hopefully the following observations can help someone else. 经过一些测试后,我发现我的问题是关于如何在PHPDoc中声明属性的大小写相关 - 希望以下观察可以帮助其他人。 This is what I have to declare my model class: 这是我必须声明我的模型类:

class Custom extends CI_Model {}

In my controller I load and use the model for example the following way: 在我的控制器中,我加载并使用模型,例如以下方式:

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

Now for phpStorm to pick up this class correctly I originally added a PHPDoc @property comment above a core class as described by others (either above the CI_Controller class or separate CI_phpStrom.php file) like this: 现在让phpStorm正确地选择这个类我最初在其他人(在CI_Controller类或单独的CI_phpStrom.php文件之上)描述的核心类上面添加了一个PHPDoc @property注释,如下所示:

*
* @property Custom $custom
*

However, this didn't remove the issue, as the variable name becomes case sensitive in this case and I had to write: 但是,这并没有消除这个问题,因为变量名在这种情况下变得区分大小写,我不得不写:

*
* @property Custom $Custom
*

for my above controller code to pick up the class correctly. 为我上面的控制器代码正确地拿起类。 An alternative would be to use lowercase when calling functions (this works even if your model declaration used uppercase) 另一种方法是在调用函数时使用小写(即使你的模型声明使用大写,这也有效)

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

The funny thing all this uppercase or lowercase didn't matter if I call my model class "Custom_model" - then it made no change if set the PHPDoc property variable to $Custom_model or $custom_model ... 如果我调用我的模型类“Custom_model”,所有这个大写或小写的有趣的事情并不重要 - 如果将PHPDoc属性变量设置为$ Custom_model或$ custom_model,它就没有变化......

Normally most people add the ' _model ' suffix to the Model class names. 通常,大多数人将“ _model ”后缀添加到Model类名称中。 I suggest that you rename your model with "Person_model" call your model "person_model" like this: 我建议您使用“Person_model”重命名模型,调用模型“person_model”,如下所示:

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

I think that you might find it an adequate solution! 我想你可能会发现它是一个合适的解决方案!

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

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