NewbieCoder
NewbieCoder

Reputation: 706

Yii, php foreach error

I have this line to query what I need, in a function of a model called Payment.php,

$sIDs = Yii::app()->db->createCommand('SELECT service_id FROM booking WHERE DATE_FORMAT(date, "%m-%Y") = "06-2015" AND complete = 1 AND service_id IS NOT NULL')->queryAll();

And then my foreach to iterate through the array list of $sIDs:

foreach ($sIDs as $sID){
        $serMod = Service::model()->findByPk($sID); //This is line 45
        $serPrice = $serMod->defaultPrice; 
        $serviceIncome += $serPrice; 
}

Unfortunately, it shows me an error:

Fatal Error: Class 'Service' not found in F:\xampp\htdocs\wellness\protected\models\Payment.php on line 45

Fatal Error: Class 'CListIterator' not found in F:\xampp\htdocs\yii\framework\collections\CList.php on line 90

I've searched through the Internet, looking at the syntax for foreach but I'm having the correct syntax already. Is there some point that I might have left out? Please guide me, thanks.

EDIT

After further debugging, I was using PHPExcel all along with this line:

spl_autoload_unregister(array('YiiBase','autoload'));   

this basically disables anything calling a Yii function or anything.

Upvotes: 0

Views: 345

Answers (4)

channasmcs
channasmcs

Reputation: 1156

hi is this for get excel report .Yii was looking for the CList.php file after the PHP script was done executin

spl_autoload_unregister(array('YiiBase','autoload'));

This allowed PHPExcel to properly execute and fill the httpRequest

The solution to the problem was fairly simple once I figured this out

$phpExcelPath = Yii::getPathOfAlias('ext.phpexcel.Classes'); //get path for PHPExcel

spl_autoload_unregister(array('YiiBase','autoload')); 
include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');
//do PHPExcel operations: load file, parse and create array, echo JSON array
spl_autoload_register(array('YiiBase','autoload')); //reload Yii before 

REF :Using XMLHttpRequest object to PHP, responseText is correct array, but 500 error thrown. Cannot identify cause of error

Upvotes: 0

NewbieCoder
NewbieCoder

Reputation: 706

I have been using PHPExcel with this line:

spl_autoload_unregister(array('YiiBase','autoload'));   

So, of course calling a function which needs other models will be disable. For example calling a function to create a model Servce::model()->findByPk() will be disabled.

Therefore, do the appropriate function before the line above and attach it to a variable. Now you can use it anywhere in the controller's action.

Upvotes: 0

Kancho Iliev
Kancho Iliev

Reputation: 701

I think you have to check for consistency your database. I mean to check if there some records in table booking which service_id key isn't present in table service ( probably this is the table behind model Service ). You can check this by something query like:

SELECT service_id FROM booking WHERE (service_id IS NOT NULL ) AND service_id NOT IN ( SELECT service_id FROM service );

Also I think you have to mentioned row to be like this:

$serMod = Service::model()->findByPk($sID['service_id']);

It is because event you have only one column in query the QueryAll returns it in array of 1-dimension arrays.

Upvotes: 0

Ron
Ron

Reputation: 6591

The problem is not in foreach, but that the Class is not found,

Since I do not see the code loaded before the foreach cycle, I cannot tell you why exactly Service is not found. Try to solve that. ANd if you do, try to vardump or print_r the class, to get a better understanding of what it contains

Upvotes: 1

Related Questions