Php Yii数据库中4个表的一个actionIndex()

Php Yii数据库中4个表的一个actionIndex(),php,mysql,sql,activerecord,yii,Php,Mysql,Sql,Activerecord,Yii,我的数据库中有4个表,每个表都包含(id,name)列 型号: 表1=id,名称; 表2=id,名称 我想在GeneralController中为每个表执行一个actionIndex()和一个view文件,但我不知道如何执行 public function actionIndex() { $model = Table1::model()->findAll(); $this->render('index', array('model'=>$model); }

我的数据库中有4个表,每个表都包含(id,name)列

型号:

表1=id,名称; 表2=id,名称

我想在GeneralController中为每个表执行一个actionIndex()和一个view文件,但我不知道如何执行

public function actionIndex() {
     $model = Table1::model()->findAll();
     $this->render('index', array('model'=>$model);
}
可能会在actionIndex($id)中发送参数以定义表和调用函数

if($id==1) {}

但也许我也会有10-15个表,其中包含(id,列)

您可以将几个变量传递给视图:

public function actionIndex() {
    $model = Table1::model()->findAll();
    $model2 = Table2::model()->findAll();
    $this->render('index', array('model'=>$model,'model2'=>$model2);
}

在这种情况下你应该这样做

public function actionIndex($name)
 {
$name=ucfirst(strtolower(rtrim(trim(strip_tags($name)))));
$allTables=array('Table1','Table2','Table3');
if(in_array($name,$allTables))
{
$model = $name::model()->findAll();
$this->render('index', array('model'=>$model);
}
else
{
//render any default view incase if the $name does not belong to any table
}
}

通过这种方式,您可以包含任意数量的模型

public function actionIndex() {
    $model1 = Table1::model()->findAll();
    $model2 = Table2::model()->findAll();
    $model3 = Table3::model()->findAll();

    $params = array('model1'=>$model1,'model2'=>$model2, 'model3'=>$model3);
    $this->render('index', $params);
}