Yii Framework

Padawan Guide

Created by Jorge Barata González

Overview

  • Fundamentals
  • Models
  • Database
  • Scaffolding

MVC

MVC

Model View Controller

MVC

Web Applications

  • Most common architecture for World Wide Web in all major programming languages
  • Application frameworks enforce the pattern

Yii Framework

Yii Framework

Documentation


http://www.yiiframework.com/doc


The Definitive Guide to Yii

The Yii Blog Tutorial

Yii Framework

MVC

Yii Framework

Installation


git clone git@github.com:yiisoft/yii.git
					

Creating Application


yii/framework/yiic webapp ./padawan
					

Create dummy database


mkdir data
touch data/testdrive.db
					

Yii Framework

Directory Structure


assets
protected/
	components
	config
	controllers	
	models 		
	runtime
	views 		
					

Yii Framework

Directory Structure


assets
protected/
	components
	config
	controllers	*
	models 		*
	runtime
	views 		*
					

Yii Framework

Workflow

Workflow

Workflow

Entry script


<?php 
// remove the following line when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// include Yii bootstrap file
require_once('path/to/yii/framework/yii.php');
// create application instance and run
$configFile='protected/config/main.php';
Yii::createWebApplication($configFile)->run();
					

Configuration

Array based:


<?php return array(...
					

Required by the entry script:


index.php         ->   protected/config/main.php
index-test.php    ->   protected/config/test.php
					

Workflow

Create a new action named padawan inside site controller

protected/controllers/SiteController.php


<?php
class SiteController extends Controller {
	...
	public function actionPadawan() {
		echo 'May the force be with you';
	}
}
					

http://localhost/lightsaber/?r=/site/padawan

Yii Framework

Model

Models

CModel

  • Attributes
  • Rules
  • Validation
  • Scenario

Form Models

CFormModel

  • Extends CModel
    • Default attribute names


protected/models/ContactForm.php

Yii Framework

Migrations

Migrations

Database schema versioning

  • Class based, extends CDbMigration
    • up()
    • down()

Migrations

m<timestamp>_<name>


<?php 
class m130719_124500_lightsaber extends CDbMigration
{
    public function up()
    {
        $this->createTable('Lightsaber', array(
            'id' => 'pk',
            'color' => 'string NOT NULL',
        ));
        $this->execute('INSERT INTO ');
    }
 
    public function down()
    {
        $this->dropTable('Lightsaber');
    }
    ...
					

Migrations

Example


#Create migration
protected/yiic migrate create lightsaber
					

...edit migration...


#Apply migration
protected/yiic migrate

#Revert migration
protected/yiic migrate down
					

Active Record

Active Record

Architectural pattern for database access

  • Class → Table
  • Object → Record


Object Record
New Insert
Modify properties Update
Load Select

Active Record ∈ ORM (Object Relational Mapping)

Active Record

Cons

  • Unit testing an active record object without a database becomes difficult

Yii Framework

Active Record

Active Record

Yii Framework

CActiveRecord

  • Extends CModel
    • Default attribute names
    • Database access
      • find()
      • save()
      • delete()
      • getRelated()
      • ...

Active Record

Yii Framework

protected/models/Lightsaber.php


<?php 
class Lightsaber extends CActiveRecord {
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}
					

Active Record

Yii Framework

model() returns an AR instance that is used to access class-level methods (something similar to static class methods) in an object context

Active Record

Yii Framework


<?php
#New row
$model = new Lightsaber;
$model->color = 'blue';
$model->save();

#Update row
$model = Lightsaber::model()->find('color=:color', array(':color'=>'blue'));
$model->color = 'red';
$model->save();

#Delete row
$model = Lightsaber::model()->findByAttributes(array('color'=>'red'));
$model->delete();
					

Active Record

Yii Framework

				count()
				exists()

				find()
				findByPk()
				findAll()
				findAllByAttributes()

				updateAll()
				updateByPk()
				updateCounters()

				deleteAll()
				deleteByPk()
					

Yii Framework

Gii

Gii

Web-based code generation tool

  • Controllers
  • CRUD
  • Form
  • Model
  • ...

Gii

protected/config/main.php


<?php
return array(
    ...
    'modules'=>array(
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'darth vader',
            ...
        ),
    ),
    ...
);
					

Gii

http://localhost/lightsaber/?r=/gii

  • Generate the Lightsaber model
  • Generate CRUD
  • http://localhost/lightsaber/?r=/lightsaber

THE END