Get all of model names in Yii

2019-09-16 19:25发布

问题:

I am creating a module for our web application. I am using Yii 1.1.14. How do I retrieve all of the model names?

I tried this but still I got no luck.

回答1:

I am not sure if my understanding of your question is correct. If you have to retrieve the name of all your models, then maybe your approach to the problem at hand is not optimal? Anyway here is what i would approach the problem:

foreach(glob('./protected/modules/<module-name>/models/*.php') as $filename){
    echo str_replace(".php", "", $filename)."<br/>";
}

This iterates through all files in your models sub-folder within your module and displays them after removing the ".php" part. If you want to access all models for the entire project change

glob('./protected/modules/<module-name>/models/*.php')

to

glob('./protected/models/*.php')