Welcome

to

Yii Training

Prepared by

Shamsher Ansari

Day 3

  1. Working with database
    • DB connectivity
    • Query builder
    • Active record
    • Relational active record

Working with Database

  • Yii use PDO.
  • Different ways to access DB.
    1. Data Access Objects (DAO)
    2. Query Builder
    3. Active Record
    4. Relational Active Record

DB connectivity

  • SQLite: sqlite:/path/to/dbfile
  • MySQL: mysql:host=localhost;dbname=testdb
  • PostgreSQL:pgsql:host=localhost;port=5432;dbname=testdb
  • SQL Server: mssql:host=localhost;dbname=testdb
  • Oracle: oci:dbname=//localhost:1521/testdb

Query builder

  • Object-Oriented way of writing SQL statements.
  • Allows building complex SQL statements programmatically.
  • Prevent conflict with SQL reserved words using "quotes".
  • Uses parameter binding, reduce risk of SQL injection attacks.
  • Offers DB abstraction, simplifies migration to different DB.

Active record

  • Active Record is popular ORM technique.
  • Removes writing SQL queries.
  • Allow access to data in more object-oriented way.
  • AR class represents DB table.
  • Class properties represent Table column.

Relational Active Record

  • Fetch data from multiple tables through Relations.
  • AR use primary-foreign key constraints
  • Maintain consistency and integrity
  • Lazy Loading
  • Eager Loading

Types of Relations in Yii

  • BELONGS_TO
  • HAS_MANY
  • HAS_ONE
  • MANY_MANY

ER Diagram

Lets do some practical experiment

Questions ?

Ask Google

"No one knows better then Google"

THANK YOU

Shamsher Ansari