Yii Framework Forum: Autoloading with wildcards is not working in ConsoleCommand - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Autoloading with wildcards is not working in ConsoleCommand Yii 1.1.8 Rate Topic: -----

#1 User is offline   Sebastian K. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 29-May 11

Posted 25 October 2011 - 12:59 PM

Hi!

I am creating a testdrive application from scratch via console as descried here: http://www.yiiframew...start.first-app

Then I change my config/console.php to:
return array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
	'name'=>'My Console Application',
	
	// autoloading model and component classes
	'import'=>array(
		'application.models.*',
		'application.components.*',
	),
	
	// application components
	'components'=>array(
		'db'=>array(
			'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
		),
	),
);


I created a class in protected/models:

class Foo {
	
	public function foo() {
		echo "foo";
	}
}


My console command looks like that:

class FooCommand extends CConsoleCommand  {
	
	public function actionIndex() {
		$foo = new Foo();
		echo $foo->foo();
	}
	
}


Finally on my console I call:
yiic foo


And get:
Fatal error: Class 'Foo' not found in C:\ProgrammeNI\xampp2\htdocs\testdrive\pro
tected\commands\FooCommand.php on line 5

Call Stack:
    0.0008     323712   1. {main}() C:\ProgrammeNI\xampp2\htdocs\testdrive\prote
cted\yiic.php:0
    0.0017     334784   2. require_once('C:\ProgrammeNI\xampp2\htdocs\yii\framew
ork\yiic.php') C:\ProgrammeNI\xampp2\htdocs\testdrive\protected\yiic.php:7
    0.0197    1272456   3. CApplication->run() C:\ProgrammeNI\xampp2\htdocs\yii\
framework\yiic.php:33
    0.0197    1272456   4. CConsoleApplication->processRequest() C:\ProgrammeNI\
xampp2\htdocs\yii\framework\base\CApplication.php:158
    0.0197    1272456   5. CConsoleCommandRunner->run() C:\ProgrammeNI\xampp2\ht
docs\yii\framework\console\CConsoleApplication.php:88
    0.0222    1403352   6. CConsoleCommand->run() C:\ProgrammeNI\xampp2\htdocs\y
ii\framework\console\CConsoleCommandRunner.php:63
    0.0224    1404352   7. ReflectionMethod->invokeArgs() C:\ProgrammeNI\xampp2\
htdocs\yii\framework\console\CConsoleCommand.php:135
    0.0224    1404368   8. FooCommand->actionIndex() C:\ProgrammeNI\xampp2\htdoc
s\testdrive\protected\commands\FooCommand.php:0


The complete project is attached. Auto-loading works fine in my web applications.

Following lines added to FooCommand working as follows:

Yii::import('application.models.Foo'); //WORKS
Yii::import('application.models.*'); //FATAL ERROR


Why is the wildcard not working for my console?

Attached File(s)


0

#2 User is offline   Sebastian K. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 29-May 11

Posted 26 October 2011 - 07:00 AM

I am using Xammp 1.74 with PHP 5.3.5 on Windows 7.
Furthermore I did some debugging in YiiBase::import() and the full path to proctected/models is actually added to the PHP include_path.

This is very strange!
0

#3 User is offline   Sebastian K. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 29-May 11

Posted 26 October 2011 - 07:41 AM

I did a little more research and this is a workaround for me:
	public function run($args) {
		Yii::$enableIncludePath = false;
		parent::run($args);
	}


But I still don't understand why
include('Foo.php');
fails when the include path to protected/models is set correctly (and works perfect on all CWebApplications).
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users