0

I don't like to declare every model as

use app\models\Country; 

Really it bothers me a lot. I like the approach used in Yii 1.15 when you could load all models using import instruction in config like: 'import' => array( 'application.models.*', )

Yes, I know it's not good for performance. But I have not more than 50 models and I care much more about my own performance rather than of performance of machine.

I had no luck in figuring out how to do it Yii2. All I found out is that it should be done via bootstrap option in main config file.

I tried the following:

 $config = [
   'language' => 'en',
   'id' => 'basicUniqueApp',
   'basePath' => dirname(__DIR__),

   'bootstrap' => [
     'log',
     'app\models\*'            
   ],

But it's not proper syntax.

Any ideas?

Tebe
  • 3,176
  • 8
  • 40
  • 60
  • There is an answer how to use classes wo namespaces http://stackoverflow.com/questions/28128117/using-classes-without-namespace-with-yii2 – SiZE Aug 20 '15 at 04:14
  • try namespace `namespace app\models;` – Insane Skull Aug 20 '15 at 05:59
  • @CrazzySkulll , does it really work for you? – Tebe Aug 20 '15 at 11:17
  • yes, it worked for me. – Insane Skull Aug 20 '15 at 11:18
  • Declaring classes with "use" is something your IDE should do for you. Besides, once your class names begin to overlap (for example, yii\sphinx\ActiveRecord and yii\db\ActiveRecord) you'll quickly appreciate the power of namespaces. – Beowulfenator Aug 20 '15 at 17:18
  • Netbeans does prefixing instead of me. I don't say that namespaces are not necessary. I want to have a choice to use them only when I feel it's necessary. Programming is freedom. I feel me forced when I can't choose. – Tebe Aug 20 '15 at 18:47
  • @SiZE this is not answer, it's bad idea to declare all models manually. In that case he has only one class to import. I have many classes. That's the difference. Yii1.5 had importing by regex – Tebe Aug 20 '15 at 19:01

1 Answers1

0

You're trying to break down PHP namespace. That's not a good idea.
If you don't want declare on top model, You can call directly without declare like this:

$country = new \app\models\Country();
Ngô Văn Thao
  • 3,671
  • 1
  • 20
  • 24
  • looks like a solution, but it makes code polluted with longer names (of course, it's good for big projecst but not for tiny ones. More fuss than actual use) – Tebe Aug 19 '15 at 19:11