Model Not Found....but Then Found, Or Not

I am having trouble understanding the nature of the issue I am facing.

Environment

I have a module, and am working with models that exist only inside that very same module.

I have a page displaying information and a search form with a From and To fields.

When the user submits the form, an Excel file is created and returned.

The issue

In my controller, I intercept if a POST request occured and proceed to search within the model for records which match the "From" and "To" selected dates.

The thing is, when I do: (the model will be called "Model" for the example)


$records = Model::model()->findAll('from > :from AND to < :to)', array(":from" => $from, ":to" => $to));

(where $from and $to are the $_POST respective parameters)

What I get is:


include(Model.php): failed to open stream: No such file or directory

on that line.

However, if before that very same line I put:


$model_test= Model::model()->find();

and then test it out, the "No such file or directory" error no longer appears.

I am however left with the same error on a line that implicates a relationship.

(note that the relations are working in other actions within the same controller)

Any tips understanding this behaviour are appreciated,

Regards

The error is raised within the autoloader. If a class does not exists it tries to load the class. you should specify the include locations within the "import" array of the config file.

Could you provide the code where you intercept? I think it may be to early. Intercepting before the module is actually loaded could result in such an error.

The Modules structure is:


--modules

    --correspondence


    --ec

      --controllers

      --models

      --modules

         --faults

         --resources

         --(...)

         --dailyRegistry        <-- module in question

           --controllers

             --DefaultController.php

           --models

             --DailyRegistry.php

           --views

             --(...)

           --DailyRegistry.php <-- end of module in question

      --views

      EcModule.php


    --<other modules>

    -- ...

In my config->main.php:


	'import'=>array(

	  'application.models.*',

          'application.models.backend.*',

	  'application.components.*',

          'application.modules.files.*',

          'application.modules.ec.*',  <-- included the main "top-level" module

          'application.modules.rh.*',

          '(...)

	),

In my EcModule.php:


	public function init()

	{

          $this->setImport(array(

	    'ec.models.*',

	    'ec.components.*',

	  ));

                

           $this->setModules(array('faults', 'resources', (...), 'dailyRegistry')); <-- included here the dailyRegistry submodule

	}

In my DailyRegistryModule.php:


	public function init()

	{

		$this->setImport(array(

                    'dailyRegistry.models.*',

                    'dailyRegistry.components.*',

                    'application.modules.ec.modules.gestaoPDA.models.*' <-- another model I had to use a certain time in an action inside the default controller but i believe the conflict is not here

		));

	}

Sorry for the long post.

Now for the action in question:


public function actionSearch(){


  if(isset($_POST["yt0"])){ //if a search is performed


    // ini_set for PHP Excel here

    // (...)

    include 'PHPExcel.php';

    include 'PHPExcel/Writer/Excel2007.php';


    $objPHPExcel = new PHPExcel();

    $objWorkSheet = $objPHPExcel->getActiveSheet()->setTitle('Team Records');


    //for this examples sake, the $from and $to will be:

    $from = $_POST["from"]." 00:00:00";

    $to = $_POST["to"]." 23:59:59";


    $records = DailyRegistry::model()->findAll('from > :from AND to < :to)', array(":from" => $from, ":to" => $to));

     //crashes here


     //however, if I put 

     $r_teste = DailyRegistry::model()->find();

     $records = DailyRegistry::model()->findAll('from > :from AND to < :to)', array(":from" => $from, ":to" => $to));

     //No crash!


     //But later on inside a foreach


     $record->team->name; //<-- crashes here as well


     //however, if I put 

     $team_test = Team::model()->find();

     $record->team->name; //<-- no crash

     

//------------------


   (...)

  }

  (...)

}

It seems it is not loading any models at all, but only in this action. In the other DefaultControllers actions there was no issue.

I really didn’t want to leave the “$dummy_test_model = Model::model()->find();” in there and I presume updating the framework won’t resolve the issue either :huh:

Regards

(any typos found in model names are probably due to translating to english for posting here)

The search action is not in the module right?

Try to comment out the phpexcel stuffs:




// include 'PHPExcel.php';

// include 'PHPExcel/Writer/Excel2007.php';


// $objPHPExcel = new PHPExcel();

// $objWorkSheet = $objPHPExcel->getActiveSheet()->setTitle('Team Records');



maybe the PHPExcel’s autoloader is the problem.

Can you copy here the error’s stack trace?

You are passing a string as the first parameter to findAll; however, this function calls for an object as the first parameter.

Try




    $criteria = new CDbCriteria;

    $criteria->addCondition("from > $from AND to < $to");

    $records = DailyRegistry::model()->findAll($criteria);



->The search action is inside the module (in the DefaultController).

-> The result is the same: failed to open stream: No such file or directory

-> I commented out the PHPExcel autoloader and the result is the same: failed to open stream: No such file or directory

StackTrace is Basically (I attach all of the relevant parts in txt 5055

StackTrace_translated.txt
):


#0	

–  C:\wamp\www\yii\framework\YiiBase.php(421): YiiBase::autoload()

416                             break;

417                         }

418                     }

419                 }

420                 else

421                     include($className.'.php'); <-------- ERROR HERE !!!!!!

422             }

423             else  // class name with namespace in PHP 5.3

424             {

425                 $namespace=str_replace('\\','.',ltrim($className,'\\'));

426                 if(($path=self::getPathOfAlias($namespace))!==false)

#1	

 unknown(0): YiiBase::autoload("DailyRegistry")

#2	

–  C:\wamp\www\examplename\protected\modules\ec\modules\dailyRegistry\controllers\DefaultController.php(259): spl_autoload_call("DailyRegistry")


258             

259 ERROR HERE-> $records = DailyRegistry::model()->findAll('from > :from AND to < :to)', array(":from" => $from, ":to" => $to));  <-------- ERROR HERE !!!!!!

260             

261             $objWorkSheet->setCellValue("A5", "Entry Date");

262             $objWorkSheet->setCellValue("B5", "Leave Date");

Any tips? Thanks for your help

Did you try to include

‘application.modules.ec.modules.*’

as well?

In my config->main.php? Yes, the result was the same.

I guess I will have to keep those ::model->find() calls.

I know this can’t be the way the framework works and most likely I have made a mistake… but the mistake seems to be hiding too well :mellow:

Thank you again for all your help and time, if I ever get to the bottom of this, I will post it.

For now I’m moving on with the tasks I have to do, maybe tomorrow I will come with something up for this behaviour :)