Namespaced Modles Problem

Hi,

i have a little problem when using namespace in model class. The model has relation defined and when i want to access this relation data error is thrown:

include(../foo/models/Iterator.php): failed to open stream: No such file or directory

Model is defined like this:

namespace foo\models;

class Bar extends \CActiveRecord { … }

So the Iterator is not using global namespace here but the one defined in model Bar. Any idea is it possible to fix it?

Silly suggestion perhaps, but what if you structure your model class file something like this:




namespace foo\models

{

    class Bar extends \CActiveRecord

    {

        ...

    }

}



Nope. This change nothing from what i see. I think i have to dig into Yii code and figure out why Iterator class that CListIterator extends uses model namespace here.

what do you mean "this change nothing"? That you tried it and you still get exactly the same symptoms for the problem?

Add:




Yii::setPathOfAlias('foo', [path/to/dir/with/foo/root]);



to your config file and all your namespaced models will start work.

creocoder your note from github (github.com/yiisoft/yii/issues/2628) helped (my class name used in relation was missing full name). This changed error though and now i have:

include(../application/components/Iterator.php): failed to open stream: No such file or directory

Maybe i am missing something more as im rewriting existing app to use namespaces.

creocoder do you have any problems with views for namespaced widgets ?

Just in case you or anyone else is still having this issue…

I had a similar problem when writing my own attribute validator class which was configured as follows:

  • Declared the class in a namespace "com\myproject\web\model\validator"

  • Put the statement “Yii::setPathOfAlias(‘com’,dirname(FILE).’\…\com’)” in the main config file

  • Registered the validator using “array(‘capacity’, ‘com\myproject\web\model\validator\ConditionallyRequiredValidator’, …),”

Whenever I tried to load the CFormModel that was using this validator, I had that “failed to open stream: No such file or directory” for the Iterator class. This was odd because the spl_autoload_call function was being given the argument ‘com\myproject\web\model\validator\Iterator’, apparently by a php internal function or something, so there was no way for Yii to figure out the correct path because the autoload call was wrong in the first place.

I had decided to drop the namespace on this validator class and just put the class on my protected\validators folder and added that folder to the Yii imports.

Today however I realised php 5.5.8 was available (my version was 5.5.7) so I decided to look at the change log and noticed the "Added validation of class names in the autoload process" so I installed this version on my server, put the validator back on its namespace with the same configuration as before and all of a sudden it just started to work. I believe this should work as well for model classes.