Getting data from a database

Most applications use databases today. Be it a small website or a social network, at least some parts are powered by databases.

Yii introduces three ways to allow you to work with databases. They are as follows:

  • 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. Also, we will compare the execution time and memory usage to determine in which cases we should use these methods.

Getting ready

  1. Create a new application using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
  2. Download the Sakila database from http://dev.mysql.com/doc/index-other.html ...

Get Yii2 Application Development Cookbook - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.