Basic Model And Directy Query

Sorry to ask about basic thing because i’m newbie. I want to know where should we get data from database, from model or direct query through Query Builder. I’m still confuse about the purpose model :unsure:

The model (CActiveRecord) maps a table record to an object.

gii will autogenerate the necessary code for view/create/update/delete a db-record based on the model.

Take a look at the code generated by yii:

Model::search, AdminController::actionAdmin/ create …

You use the querybuilder and dao as a direct way to work with db-data without generating a model for each record.

This is better performance if you want to list/update (complex, joined) db-data.

Thx for explained it :D .

So if I want to show the list of a product or something like that it will be better to use Query Builder, while if I want to insert data to a table it’s better to use the model that have internal validation


Model is used for any time of DB operations. Whether it is related to displaying dat or inserting or deleting data from DB.

Custom query builder takes more time and extra variables to define query builder. So better to use model for any type of DB operations.