遇到个奇怪问题,关于AR+循环读取 [ 新手入门 ]
异常:我写了一段代码,读取一次正常,多次读取就提示无法读取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这样的取属性就直接报错。。。。
实在不解。。。求解。。。是不是哪我忽略了?
共 3 条回复
BoyLee
注册时间:2011-03-10
最后登录:2019-12-26
在线时长:20小时5分
最后登录:2019-12-26
在线时长:20小时5分
- 粉丝22
- 金钱2810
- 威望150
- 积分4510