Download yii-guide-1.1.14 PDF

Titleyii-guide-1.1.14
File Size1.1 MB
Total Pages273
Table of Contents
                            Contents
License
Getting Started
	The Definitive Guide to Yii
	New Features
		Version 1.1.14
		Version 1.1.11
		Version 1.1.8
		Version 1.1.7
		Version 1.1.6
		Version 1.1.5
		Version 1.1.4
		Version 1.1.3
		Version 1.1.2
		Version 1.1.1
		Version 1.1.0
	Upgrading from Version 1.0 to 1.1
		Changes Related with Model Scenarios
		Changes Related with Eager Loading for Relational Active Record
		Changes Related with Table Alias in Relational Active Record
		Changes Related with Tabular Input
		Other Changes
	What is Yii
		Requirements
		What is Yii Best for?
		How does Yii Compare with Other Frameworks?
	Installation
		Requirements
	Apache and Nginx configurations
		Apache
		Nginx
	Creating Your First Yii Application
		Connecting to Database
		Implementing CRUD Operations
Fundamentals
	Model-View-Controller (MVC)
		A Typical Workflow
	Entry Script
		Debug Mode
	Application
		Application Configuration
		Application Base Directory
		Application Components
		Core Application Components
		Application Life Cycle
	Controller
		Route
		Controller Instantiation
		Action
		Filter
	Model
	View
		Layout
		Widget
		System View
	Component
		Defining and using a Component Property
		Component Event
		Component Behavior
	Module
		Creating Module
		Using Module
		Nested Module
	Path Alias and Namespace
		Root Alias
		Importing Classes
		Importing Directories
		Namespace
		Namespaced Classes
		Namespaced Controllers
		Namespaced Modules
	Conventions
		URL
		Code
		Configuration
		File
		Directory
		Database
	Development Workflow
	Best MVC Practices
		Model
		View
		Controller
Working with Forms
	Working with Form
	Creating Model
		Defining Model Class
		Declaring Validation Rules
		Securing Attribute Assignments
		Triggering Validation
		Retrieving Validation Errors
		Attribute Labels
	Creating Action
	Creating Form
	Collecting Tabular Input
	Using Form Builder
		Basic Concepts
		Creating a Simple Form
		Specifying Form Elements
		Accessing Form Elements
		Creating a Nested Form
		Customizing Form Display
Working with Databases
	Working with Database
	Data Access Objects (DAO)
		Establishing Database Connection
		Executing SQL Statements
		Fetching Query Results
		Using Transactions
		Binding Parameters
		Binding Columns
		Using Table Prefix
	Query Builder
		Preparing Query Builder
		Building Data Retrieval Queries
		Building Data Manipulation Queries
		Building Schema Manipulation Queries
	Active Record
		Establishing DB Connection
		Defining AR Class
		Creating Record
		Reading Record
		Updating Record
		Deleting Record
		Data Validation
		Comparing Records
		Customization
		Using Transaction with AR
		Named Scopes
	Relational Active Record
		Declaring Relationship
		Performing Relational Query
		Performing Relational query without getting related models
		Relational Query Options
		Disambiguating Column Names
		Dynamic Relational Query Options
		Relational Query Performance
		Statistical Query
		Relational Query with Named Scopes
		Relational Query with through
	Database Migration
		Creating Migrations
		Transactional Migrations
		Applying Migrations
		Reverting Migrations
		Redoing Migrations
		Showing Migration Information
		Modifying Migration History
		Customizing Migration Command
Caching
	Caching
	Data Caching
		Cache Dependency
		Query Caching
	Fragment Caching
		Caching Options
		Nested Caching
	Page Caching
		Output Caching
		HTTP Caching
	Dynamic Content
Extending Yii
	Extending Yii
	Using Extensions
		Zii Extensions
		Application Component
		Behavior
		Widget
		Action
		Filter
		Controller
		Validator
		Console Command
		Module
		Generic Component
	Creating Extensions
		Application Component
		Behavior
		Widget
		Action
		Filter
		Controller
		Validator
		Console Command
		Module
		Generic Component
	Using 3rd-Party Libraries
		Using namespaced 3rd-Party Libraries
		Using Yii in 3rd-Party Systems
Testing
	Testing
		Test-Driven Development
		Test Environment Setup
		Test Bootstrap Script
	Defining Fixtures
	Unit Testing
	Functional Testing
Special Topics
	Automatic Code Generation
		Using Gii
		Extending Gii
	URL Management
		Creating URLs
		User-friendly URLs
		Using Named Parameters
		Parameterizing Routes
		Parameterizing Hostnames
		Hiding index.php
		Faking URL Suffix
		Using Custom URL Rule Classes
	Authentication and Authorization
		Defining Identity Class
		Login and Logout
		Cookie-based Login
		Access Control Filter
		Handling Authorization Result
		Role-Based Access Control
		Configuring Authorization Manager
		Defining Authorization Hierarchy
		Using Business Rules
	Theming
		Using a Theme
		Creating a Theme
		Theming Widgets
		Customizing Widgets Globally
		Skin
	Logging
		Message Logging
		Message Routing
		Message Filtering
		Logging Context Information
		Performance Profiling
		Profiling SQL Executions
	Error Handling
		Raising Exceptions
		Displaying Errors
		Handling Errors Using an Action
		Message Logging
	Web Service
		Defining Service Provider
		Declaring Web Service Action
		Consuming Web Service
		Data Types
		Class Mapping
		Intercepting Remote Method Invocation
	Internationalization
		Locale and Language
		Translation
		Date and Time Formatting
		Number Formatting
	Using Alternative Template Syntax
		Using CPradoViewRenderer
		Mixing Template Formats
	Console Applications
		Overview
		Creating Commands
		Console Command Action
		Exit Codes
		Customizing Console Applications
	Security
		Cross-site Scripting Prevention
		Cross-site Request Forgery Prevention
		Cookie Attack Prevention
	Performance Tuning
		Enabling APC Extension
		Disabling Debug Mode
		Using yiilite.php
		Using Caching Techniques
		Database Optimization
		Minimizing Script Files
		Symlinking assets
	Code Generation using Command Line Tools (deprecated)
                        
Document Text Contents
Page 1

The De�nitive Guide to Yii 1.1

Qiang Xue and Xiang Wei Zhuo

Copyright 2008-2013. All Rights Reserved.

Page 136

120 4. Working with Databases

class Post extends CActiveRecord

{
......

public function relations()

{
return array(

’author’=>array(self::BELONGS TO, ’User’, ’author id’),

’categories’=>array(self::MANY MANY, ’Category’,

’tbl post category(post id, category id)’),

);

}
}

class User extends CActiveRecord

{
......

public function relations()

{
return array(

’posts’=>array(self::HAS MANY, ’Post’, ’author id’),

’profile’=>array(self::HAS ONE, ’Profile’, ’owner id’),

);

}
}

Info: A foreign key may be composite, consisting of two or more columns. In this

case, we should concatenate the names of the foreign key columns and separate

them with commas or an array such as array(’key1’,’key2’). In case you need

to specify custom PK->FK association you can define it as array(’fk’=>’pk’). For

composite keys it will be array(’fk c1’=>’pk c1’,’fk c2’=>’pk c2’). For MANY

MANY relationship type, the associative table name must also be specified in the

foreign key. For example, the categories relationship in Post is specified with the

foreign key tbl post category(post id, category id). The declaration of rela-

tionships in an AR class implicitly adds a property to the class for each relationship.

After a relational query is performed, the corresponding property will be populated

with the related AR instance(s). For example, if $author represents a User AR

instance, we can use $author->posts to access its related Post instances.

4.5.2 Performing Relational Query

The simplest way of performing relational query is by reading a relational property of

an AR instance. If the property is not accessed previously, a relational query will be

initiated, which joins the two related tables and filters with the primary key of the current

Page 137

4.5 Relational Active Record 121

AR instance. The query result will be saved to the property as instance(s) of the related

AR class. This is known as the lazy loading approach, i.e., the relational query is performed

only when the related objects are initially accessed. The example below shows how to use

this approach:

// retrieve the post whose ID is 10

$post=Post::model()->findByPk(10);

// retrieve the post’s author: a relational query will be performed here

$author=$post->author;

Info: If there is no related instance for a relationship, the corresponding property

could be either null or an empty array. For BELONGS TO and HAS ONE relationships,

the result is null; for HAS MANY and MANY MANY, it is an empty array. Note that the

HAS MANY and MANY MANY relationships return arrays of objects, you will need to

loop through the results before trying to access any properties. Otherwise, you may

receive ”Trying to get property of non-object” errors.

The lazy loading approach is very convenient to use, but it is not efficient in some scenarios.

For example, if we want to access the author information for N posts, using the lazy

approach would involve executing N join queries. We should resort to the so-called eager

loading approach under this circumstance.

The eager loading approach retrieves the related AR instances together with the main AR

instance(s). This is accomplished by using the with() method together with one of the

find or findAll methods in AR. For example,

$posts=Post::model()->with(’author’)->findAll();

The above code will return an array of Post instances. Unlike the lazy approach, the

author property in each Post instance is already populated with the related User instance

before we access the property. Instead of executing a join query for each post, the eager

loading approach brings back all posts together with their authors in a single join query!

We can specify multiple relationship names in the with() method and the eager loading

approach will bring them back all in one shot. For example, the following code will bring

back posts together with their authors and categories:

$posts=Post::model()->with(’author’,’categories’)->findAll();

Page 272

256 8. Special Topics

Figure 8.1: User admin page

Page 273

8.13 Code Generation using Command Line Tools (deprecated) 257

Figure 8.2: Create new user page

Similer Documents