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?

7
  • There is an answer how to use classes wo namespaces stackoverflow.com/questions/28128117/… Commented Aug 20, 2015 at 4:14
  • try namespace namespace app\models; Commented Aug 20, 2015 at 5:59
  • @CrazzySkulll , does it really work for you? Commented Aug 20, 2015 at 11:17
  • yes, it worked for me. Commented Aug 20, 2015 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. Commented Aug 20, 2015 at 17:18

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.