How Yii::import() is (should) work?

Hi there,

In Yii manual it is said that Yii::import() is much faster and convinient than PHP include(). Therefore I tried hard to use it but after wasting over half an hour on unsuccessful attempts to understand philosophy (or madness? :) beyond Yii::import() I’ve decided to ask forum for… what the heck am I missing here?

I have the simpliest class as it can be, which extends CDbConnection. I named it COracleDbConnection and placed it’s definition in file named COracleDbConnection.php directly in ‘extensions’ folder. Then tried to use it in Yii by writing to main config file this:


//Database component

        'db'=>array(

            'class'=>'COracleDbConnection',

            'connectionString'=>'oci://user:pass@host:port/db',

            'username'=>'user',

            'password'=>'pass'

        ),

For above to work I need to import class definition, what I’m doing in bootstrap file, index.php. The only problem is that:


include('protected/extensions/COracleDbConnection.php');

works fine while ANY combination of Yii::import() fails. I’ve spent over half an hour reading, coding and testing so I believe I tested any combination. A few of tested were:


Yii::import('application.extensions.COracleDbConnection');

Yii::import('ext.COracleDbConnection');

Yii::import('application.extensions.*');

Yii::import('ext.*');

[tip: tested on HTTPD server running on Windows, therefore filename case does not matter]

In every attempt I get either “unhandled CExteption” that alias seems to be wrong or Yii seems to be loading file (no exception) but as soon as it start to parse config/main.php in which I call DB component with ‘class’=>‘COracleDbConnection’ I get exception saying that ‘COracleDbConnection’ class was not found.

My so-called-extension is a small class overloader, it is included in only one file therefore I decided to put it directly in ‘extensions’ folder, without a subfolder. I don’t plan to use any other extensions andI don’t like the idea of creating extra folders to hold just one file.

But, of course at some point I thought that - for some reason - Yii my require that every extension (even with one file) must be hold in separate folder. So I tested above combinations (attempts of use) of Yii:import also when ‘COracleDbConnection.php’ was put to either protected/extensions/myocidob/ or protected/extensions/test folders. No effect. Either unhandled exepction, alias is bad or so-called-load (no exception on import and then exception of first attempt of use COracleDbConnection class).

Has anyone got any idea what am I doing wrong or what am I missing here?

I think that you cannot use Yii::import for import a file that is need in configuration.

Before parsing the config file the configuration of estension dir is not loaded.

I think that the only solution is to use the manual inport, as you correctly did.

zaccaria is right. But you can try this:




'db'=>array(

            'class'=>'application.extensions.COracleDbConnection',

Also, you should avoid the C prefix for classes of custom components.

Thanks! That solves problem!

Of course, you’re right! It was a type done via quick copy and paste of original class. As they say: Copy&Paste = Time waste! :)