Getting data from a database
Most applications today use databases. Be it a small website or a social network, at least some parts are powered by databases. Yii introduces three ways that allow you to work with databases:
Active Record
Query builder
SQL via DAO
We will use all these methods to get data from the film, film_actor, and actor tables and show it in a list. We will measure the execution time and memory usage to determine when to use these methods.
Getting ready
Create a new application by using
yiic webappas described in the official guide at the following URL:http://www.yiiframework.com/doc/guide/en/quickstart.first-app
Download the Sakila database from the following URL:
Execute the downloaded SQLs; first schema then data.
Configure the DB connection in
protected/config/main.phpto use the Sakila database.Use Gii to create models for the
actorandfilmtables.
How to do it...
We will create
protected/controllers/DbController.phpas follows...