Read from a database view and save on a database table


How are you all? First of all, sry about my english, I´m from Uruguay and you know, we speak Spanish here haha

I´m new with Yii2 and I know you all can help me, epic programmers :lol:

The key is that I got to read data from a database view and save it in a database table with other rows, then show it on a GridView, the problem is that the rows have different names and I really don´t know how to set each row.

Here I want to ask if the data that I´m calling is in the table, if it is not, i´ll

read the database view and insert it in the $dataProvider, but it doesn´t work :D

public function actionIndex()


        $searchModel = new LaboratorioAlfaSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


		if ($dataProvider-> totalCount == 0) {


			$searchModel = new ViewLaboratorioAlfaSearch();

        	        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);




		return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,



Thanks thanks thanks!

Hi Thyds, welcome to the forum.

Would you please explain your database view with more details to help us understand your needs?

What does it look like? What tables does it involve?

Is LaboratorioAlfa (that you have extended LaboratorioAlfaSearch from) based on a database view or a table?

Yeah sry, I´ve got a model of my database view and a model of my database table, what I want to do is to read data on the database view and insert it in the database table.

Can u give an example of that?

Just a simple sample:

$viewModel = ViewModel::find()->...->one();

$tableModel = new TableModel();

$tableModel->attr1 = $viewModel->something;



1 Like

Oh dude, very thank you! Amazing ;D