Day 3
Working with database
- DB connectivity
- Query builder
- Active record
- Relational active record
Working with Database
- Yii use PDO.
- Different ways to access DB.
Data Access Objects (DAO)
Query Builder
Active Record
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
- 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 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.
- 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
Lets do some practical experiment
Questions ?
Ask Google
"No one knows better then Google"