我在actionView和actionIndex中添加了以下代码。代码工程代码在actionView中使用时工作正常,但在actionIndex上使用时会抛出Error 500,Undefined variable:id。为什么变量id仅在actionIndex上未定义?
actionView ...
public function actionView($id)
{
$textDataProvider=new CActiveDataProvider('Text', array(
'criteria'=>array(
'condition'=>'articles_id=:articlesId',
'params'=>array(':articlesId'=>$this->loadModel($id)->id),
),
'pagination'=>array(
'pageSize'=>1,
),
));
$this->render('view',array(
'model'=>$this->loadModel($id),
'textDataProvider'=>$textDataProvider,
));
}
相同的代码,actionIndex只有很小的变化......
public function actionIndex()
{
//get text information
$textDataProvider=new CActiveDataProvider('Text', array(
'criteria'=>array(
'condition'=>'articles_id=:articlesId',
'params'=>array(':articlesId'=>$this->loadModel($id)->id),
'offset'=>'0',
'limit' => '1',
),
));
//get criteria
$criteria = new CDbCriteria();
//$criteria->condition = 'country = :country';
//$criteria->params = array (':country'=> $_GET['country']);
$criteria->order = 'create_time desc';
//get count
$count = Articles::model()->count($criteria);
//pagination
$pages = new CPagination($count);
$pages->setPageSize(10);
$pages->applyLimit($criteria);
//result to show on page
$result = Articles::model()->findAll($criteria);
$dataProvider = new CArrayDataProvider($result);
$this->render('index', array(
'dataProvider' => $dataProvider,
'pages' => $pages,
'textDataProvider'=>$textDataProvider,
));
}
我已经尝试对actionIndex进行更改以尝试获取ID,如下所述。但我仍然可以得到身份。
actionIndex ...
public function actionIndex() {
//get criteria
$criteria = new CDbCriteria();
//$criteria->condition = 'country = :country';
//$criteria->params = array (':country'=> $_GET['country']);
$criteria->order = 'create_time desc';
//get count
$count = Articles::model()->count($criteria);
//pagination
$pages = new CPagination($count);
$pages->setPageSize(10);
$pages->applyLimit($criteria);
//result to show on page
$result = Articles::model()->findAll($criteria);
$dataProvider = new CArrayDataProvider($result);
//$arr = array();
foreach($result as $id)
{
$text_id = $id->id;
}
//get text information
$criteria2=new CDbCriteria(array(
'order'=>'id desc',
'condition'=>'articles_id='.$text_id,
));
$textProvider=new CActiveDataProvider('Text', array(
'criteria'=>$criteria2,
));
$this->render('index', array(
'dataProvider' => $dataProvider,
'pages' => $pages,
'textProvider'=>$textProvider,
));
}
答案 0 :(得分:0)
很明显,您使用$id
而没有定义它!在actionView
中,您有$id
作为参数,但在actionIndex
中您没有它。因此'params'=>array(':articlesId'=>$this->loadModel($id)
会导致未定义变量错误。
要解决此问题,您必须将$id
传递给actionIndex
。