Why this AR not work.

here is the code.

Controller


for ($i=0; $i<$roundTimes; $i++)

            $generateMath['e'][$i] = Equation::model()->generateMath($_POST['Equation']['code'], $offSetRe);



Model :: Equation :: generateMath




....switch.....


case $REVIEW_ALL:

                $reequation  = Reequation::model()->getUserReequation(0, $offSetRe);

//                print_r($reequation->first);

                $EF = $reequation->first;

                $ES = $reequation->second;

                $EO = $reequation->operator;

                $id = $reequation->id;

                break;

.....code switch.....


$arrMathElements =array('F'=>$EF , 'S'=>$ES, 'O'=>$EO, 'id'=>$id);

        return $arrMathElements;



Model Reequation :: getUserReequation




        /**

         * 

         * Get a Reequation from DB 

         * @param int $type   (1=>today || 0=>all)

         * @param int $max the max rand value

         * return 1 Reequation one times

         */

        public function getUserReequation($type = 0, $max=0)

        {

            $criteria = new CDbCriteria();

            $criteria->condition = ' uid = :uid and end_date is null ';

            $criteria->params[':uid'] = Yii::app()->user->id;


            if ($type == 1){

                $criteria->condition .= ' and add_date > :today_date ';

                $criteria->params[':today_date'] = date('Y-m-d');

            }

            

            $criteria->order = ' id DESC ';

            

            $criteria->limit = 1;

            $criteria->offset = rand(1, $max);

            

            $reEquationResult = Reequation::model()->find($criteria);

//            print_r($reEquationResult->uid);

//            print_r($reEquationResult);

            return $reEquationResult ;

        }



Yii will tell me at "$EF = $reequation->first;" Trying to get property of non-object.

I did a lot of debug…

=========1

Controller




            $generateMath['e'] = Equation::model()->generateMath($_POST['Equation']['code'], $offSetRe);



when I changed controller like this, it works fine. just remove loop why?

===========2

when keep loop and get error msg, I found used print_r($reEquationResult); in Model Reequation :: getUserReequation can get object and values from DB, but when I use print_r($reEquationResult->uid); the same error msg shows again.

who can tell me why?

thank you very much.

I solved like this




$EF = $reequation['first'];

                $ES = $reequation['second'];

                $EO = $reequation['operator'];

                $id = $reequation['id'];



But I still didn’t know why the above code work without loop, who can tell me.

Is this a bug or a typing error ?




  $reEquationResult = Reequation::model()->find($criteria);

  return $Reequation;



$Reequation is not defined in this function

is a typing error…

I was debug and mac+Z

could you tell me why use array can solved this question?