Created by Jorge Barata González
http://www.yiiframework.com/doc
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
assets
protected/
components
config
controllers
models
runtime
views
assets
protected/
components
config
controllers *
models *
runtime
views *
<?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();
Array based:
<?php return array(...
Required by the entry script:
index.php -> protected/config/main.php
index-test.php -> protected/config/test.php
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
CModel
CFormModel
CModel
protected/models/ContactForm.php
CDbMigration
up()
down()
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');
}
...
#Create migration
protected/yiic migrate create lightsaber
...edit migration...
#Apply migration
protected/yiic migrate
#Revert migration
protected/yiic migrate down
Architectural pattern for database access
Object | Record |
New | Insert |
Modify properties | Update |
Load | Select |
Active Record ∈ ORM (Object Relational Mapping)
Cons
CActiveRecord
CModel
find()
save()
delete()
getRelated()
protected/models/Lightsaber.php
<?php
class Lightsaber extends CActiveRecord {
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
model()
returns an AR instance that is used to access class-level methods (something similar to static class methods) in an object context
<?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();
count() exists() find() findByPk() findAll() findAllByAttributes() updateAll() updateByPk() updateCounters() deleteAll() deleteByPk()
Web-based code generation tool
protected/config/main.php
<?php
return array(
...
'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'darth vader',
...
),
),
...
);
http://localhost/lightsaber/?r=/gii
http://localhost/lightsaber/?r=/lightsaber