BoyLee 2011-06-19 00:50:48 3379次浏览 3条回复 0 0 0

异常:我写了一段代码,读取一次正常,多次读取就提示无法读取AR对象的属性 “Trying to get property of non-object”

看了半天实在没看懂,帖出来大家看看

一次读取无错版 Controller

$generateMath['e'] = 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 $Reequation;
}

上面的代码为1次执行一点问题都没有,但是如果做如下修改: controller

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

就直接报错 “Trying to get property of non-object” 位置 Model :: Equation :: generateMath$EF = $reequation->first;

很奇怪的,我试着debug,可以确定此时如果在 getUserReequation 使用 print_r($reEquationResult)可以看到值从数据库中读取到了,但是如果->uid这样的取属性就直接报错。。。。

实在不解。。。求解。。。是不是哪我忽略了?

  • 回复于 2011-06-19 02:13 举报
    $EF = $reequation->first;
    $ES = $reequation->second;
    $EO = $reequation->operator;
    $id = $reequation->id;
    

    解决方法是这几行改成数组。。。调用下标,但是我实在不懂,为什么一上循环就挂掉了?

  • 回复于 2011-06-19 07:45 举报

    first是数据库字段吗?

  • 回复于 2011-06-19 15:45 举报

    是字段,在一次读取的时候没有任何问题,但是不知道为何,上了一个循环就出问题了。。。虽然解决了,但是我还是不清楚,问题出在哪。。。

您需要登录后才可以回复。登录 | 立即注册