0

Here is my console.php which is same as main.php

return array(
'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
'name' => 'My Console Application',

// preloading 'log' component
'preload' => array('log'),

 // autoloading model and component classes
'import' => array(
        'application.models.*',
        'application.components.*',
        'application.extensions.*',
),

The web application has no problem access those functions. However, console application yields error

Fatal error: Call to undefined method Myfunction::get_all_recipients() 

An discussion of this problem is also found at

https://github.com/yiisoft/yii/issues/2344

Also tried suggestions

Yii Command Line Does Not Autoload Models

but it still does not import model classes.

Community
  • 1
  • 1
TED
  • 1,829
  • 5
  • 18
  • 36
  • Why the second answer didn't help? Please show what model you want to import, where is located, and what's under `import` section of your console config now. – arogachev Nov 12 '15 at 03:42
  • all the models are located under protected/models/* and the import section looks same like above no difference. – TED Nov 12 '15 at 03:44

1 Answers1

1

The second solution should work, but sometimes I use another approach. In the console command itself add the following:

Yii::import('application.models.YourModel');

or

Yii::import('application.models.Subfolder.YourModel');

if it's located in subfolder for example;

This should placed before your code is processed. This autoloads only models or classes that your need for current command.

arogachev
  • 33,150
  • 7
  • 114
  • 117