Can A Model Be Based On A View?

Can I create a YII model with a view in MYSQL?

Can you be more specific?

I’m getting aquainted with Yii2 so I’m not sure, but certainly Yii1 handles that - check out this wiki and see if you can apply its concepts to Yii2. Please let us know your results. :)

I have created a view in MySQL which is based on table1, table2 and table3

Now my frontend has a search and view functionally on which i have replaced the grid view from $model to get it from the view results.

So if my model is based on my view (which could be a table) , i can use the built in search function…

The way I understand it the MySQL view must be updateable, meaning that it can only contain one table and one primary key. You would need 3 different views and define their relationships in their respective models; then you would use those relationships to access whatever columns you need from any of the models.

Thanks but I am not sure if this answers my question.


If I had to display data and search from one single table , I could have used the CRUD functionality on the model and modified the views so that I can see the search criteria and the Data grid from the particular model

Now , I have a view which shows 4 columns from 3 different tables which is in the mysql database

I can actually call the mysqlview from the controller as

 $rawData=Yii::app()->db->createCommand('SELECT * FROM vCustomer')->queryAll(); //you are creating a command here


             $list2DataProvider=new CArrayDataProvider($rawData, array('pagination'=>false));

in example above vCustomer is an MySQL view.

but this will not allow me the flexibility that i have in a model from just one table.

Hence I wanted to know if I can create a Model based on MYSQLview.

AFAIK you can create a model from a MySQL view if you meet certain requirements, mainly that the view contains only one table. The reason for that is that Yii models require a single primary key. I’m unsure how it would behave with a 3-table view (therefore 3 PK’s) but I don’t think it’s supported. Perhaps you can create 3 new, separate views for the columns of the 3 tables in your existing view and use the traditional table approach. You can always try it and find out.

Thanks I will give it a try, but i have one more question.

If I ensure to have one PK in my MySQLview and have a new view like below


my view will be vCustomer and if I run

Select * from vCustomer 

gives me the following columns

Customerid , Customername, CustomerAdress, CustomerProduct1 

where Customerid, Customername and CustomerAdress are coming from Table1

and CustomerProduct1 is coming from Table 2.

How can I automatically generate a model out of this view using gii? I think model always asks for table right?

Gii will detect the schema and think it’s a table so it will generate the model. Just make sure you manually add the primaryKey() function in the model because Gii won’t detect it and won’t add it automatically.

Great!!! Thanks it works!!!!

another last question is on this link if you can please advice?

I am now trying to change the way , the data is displayed and i was thinking to change the code below:

<?php $this->widget('zii.widgets.CDetailView', array(



I would like to display the data in a row wise but not like a table as below

How to display data

For that kind of display I’m afraid you’ll have to code it yourself without aid from Yii’s widgets (CListView, CDetailView and CGridView). While they are extremely flexible they can’t do it all. The only thing I can think of is to custom code the CSS classes the widgets use (refer to their documentation) to change their look. And it seems like you want some sort of carrousel widget; that would also require custom code.

Sorry I got it, I think its CListView I was searching for!