2013-12-23 27 views
0

我在我的DB上有4個表,其中每個表都包含(id,name)列。Yii one actionIndex()for DB from DB

Models:

Table1 = id,name; 表2 = id,名稱;

我想爲我的GeneralController中的每個表執行1個actionIndex()和1個視圖文件,但我不知道該怎麼做。

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

也許發送PARAMS中的actionIndex($ id)則定義表和調用功能

if($id==1) {} 

但也許我將有10-15表太有(ID列)

回答

1

你可以通過幾個變量來查看:

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

你應該這樣做在這種情況下

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 
} 
} 
0

您可以像您想要的那樣包含模型。

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); 
}