The Definitive Guide to Yii
Total Page:16
File Type:pdf, Size:1020Kb
The Definitive Guide to Yii 1.1 Qiang Xue and Xiang Wei Zhuo Copyright 2008-2010. All Rights Reserved. Contents Contents i License xi 1 Getting Started1 1.1 The Definitive Guide to Yii...........................1 1.2 New Features...................................1 1.2.1 Version 1.1.2...............................1 1.2.2 Version 1.1.1...............................1 1.2.3 Version 1.1.0...............................1 1.2.4 Version 1.0.11...............................2 1.2.5 Version 1.0.10...............................2 1.2.6 Version 1.0.8...............................2 1.2.7 Version 1.0.7...............................3 1.2.8 Version 1.0.6...............................3 1.2.9 Version 1.0.5...............................3 1.3 Upgrading from Version 1.0 to 1.1.......................4 1.3.1 Changes Related with Model Scenarios.................4 1.3.2 Changes Related with Eager Loading for Relational Active Record.4 1.3.3 Changes Related with Table Alias in Relational Active Record...5 1.3.4 Changes Related with Tabular Input..................5 ii Contents 1.3.5 Other Changes..............................5 1.4 What is Yii....................................5 1.4.1 Requirements...............................5 1.4.2 What is Yii Best for?..........................5 1.4.3 How is Yii Compared with Other Frameworks?............6 1.5 Installation....................................6 1.5.1 Requirements...............................6 1.6 Creating First Yii Application..........................7 1.6.1 Connecting to Database......................... 11 1.6.2 Implementing CRUD Operations.................... 12 2 Fundamentals 17 2.1 Model-View-Controller (MVC)......................... 17 2.1.1 A Typical Workflow........................... 18 2.2 Entry Script.................................... 19 2.2.1 Debug Mode............................... 19 2.3 Application.................................... 20 2.3.1 Application Configuration........................ 20 2.3.2 Application Base Directory....................... 21 2.3.3 Application Component......................... 21 2.3.4 Core Application Components..................... 22 2.3.5 Application Lifecycles.......................... 23 2.4 Controller..................................... 24 2.4.1 Route................................... 24 2.4.2 Controller Instantiation......................... 25 Contents iii 2.4.3 Action................................... 25 2.4.4 Filter................................... 26 2.5 Model....................................... 28 2.6 View........................................ 29 2.6.1 Layout................................... 29 2.6.2 Widget.................................. 30 2.6.3 System View............................... 31 2.7 Component.................................... 31 2.7.1 Component Property........................... 31 2.7.2 Component Event............................ 32 2.7.3 Component Behavior........................... 33 2.8 Module...................................... 35 2.8.1 Creating Module............................. 35 2.8.2 Using Module............................... 36 2.8.3 Nested Module.............................. 37 2.9 Path Alias and Namespace........................... 37 2.10 Conventions.................................... 39 2.10.1 URL.................................... 39 2.10.2 Code.................................... 39 2.10.3 Configuration............................... 40 2.10.4 File.................................... 40 2.10.5 Directory................................. 40 2.10.6 Database................................. 41 2.11 Development Workflow.............................. 42 iv Contents 3 Working with Forms 45 3.1 Working with Form................................ 45 3.2 Creating Model.................................. 45 3.2.1 Defining Model Class........................... 46 3.2.2 Declaring Validation Rules....................... 46 3.2.3 Securing Attribute Assignments..................... 49 3.2.4 Triggering Validation........................... 52 3.2.5 Retrieving Validation Errors...................... 53 3.2.6 Attribute Labels............................. 53 3.3 Creating Action.................................. 53 3.4 Creating Form.................................. 55 3.5 Collecting Tabular Input............................. 57 3.6 Using Form Builder............................... 58 3.6.1 Basic Concepts.............................. 59 3.6.2 Creating a Simple Form......................... 59 3.6.3 Specifying Form Elements........................ 61 3.6.4 Accessing Form Elements........................ 64 3.6.5 Creating a Nested Form......................... 65 3.6.6 Customizing Form Display....................... 67 4 Working with Databases 69 4.1 Working with Database............................. 69 4.2 Data Access Objects (DAO)........................... 69 4.2.1 Establishing Database Connection................... 70 4.2.2 Executing SQL Statements....................... 71 Contents v 4.2.3 Fetching Query Results......................... 72 4.2.4 Using Transactions............................ 72 4.2.5 Binding Parameters........................... 73 4.2.6 Binding Columns............................. 74 4.2.7 Using Table Prefix............................ 74 4.3 Active Record................................... 74 4.3.1 Establishing DB Connection....................... 75 4.3.2 Defining AR Class............................ 76 4.3.3 Creating Record............................. 78 4.3.4 Reading Record.............................. 79 4.3.5 Updating Record............................. 82 4.3.6 Deleting Record............................. 82 4.3.7 Data Validation.............................. 83 4.3.8 Comparing Records........................... 84 4.3.9 Customization.............................. 84 4.3.10 Using Transaction with AR....................... 84 4.3.11 Named Scopes.............................. 85 4.4 Relational Active Record............................ 87 4.4.1 Declaring Relationship.......................... 88 4.4.2 Performing Relational Query...................... 90 4.4.3 Relational Query Options........................ 92 4.4.4 Disambiguating Column Names..................... 94 4.4.5 Dynamic Relational Query Options................... 94 4.4.6 Relational Query Performance..................... 95 vi Contents 4.4.7 Statistical Query............................. 96 4.4.8 Relational Query with Named Scopes................. 98 5 Caching 101 5.1 Caching...................................... 101 5.2 Data Caching................................... 103 5.2.1 Cache Dependency............................ 104 5.3 Fragment Caching................................ 105 5.3.1 Caching Options............................. 105 5.3.2 Nested Caching.............................. 107 5.4 Page Caching................................... 108 5.5 Dynamic Content................................. 109 6 Extending Yii 111 6.1 Overview..................................... 111 6.2 Using Extensions................................. 112 6.2.1 Zii Extensions............................... 112 6.2.2 Application Component......................... 113 6.2.3 Behavior.................................. 113 6.2.4 Widget.................................. 114 6.2.5 Action................................... 115 6.2.6 Filter................................... 115 6.2.7 Controller................................. 116 6.2.8 Validator................................. 116 6.2.9 Console Command............................ 117 Contents vii 6.2.10 Module.................................. 117 6.2.11 Generic Component........................... 117 6.3 Creating Extensions............................... 118 6.3.1 Application Component......................... 118 6.3.2 Behavior.................................. 119 6.3.3 Widget.................................. 119 6.3.4 Action................................... 121 6.3.5 Filter................................... 121 6.3.6 Controller................................. 121 6.3.7 Validator................................. 122 6.3.8 Console Command............................ 122 6.3.9 Module.................................. 122 6.3.10 Generic Component........................... 123 6.4 Using 3rd-Party Libraries............................ 123 7 Testing 125 7.1 Overview..................................... 125 7.2 Test-Driven Development............................ 126 7.3 Test Environment Setup............................. 126 7.4 Test Bootstrap Script.............................. 127 7.5 Defining Fixtures................................. 128 7.6 Unit Testing.................................... 130 7.7 Functional Testing................................ 132 8 Special Topics 135 viii Contents 8.1 Automatic Code Generation........................... 135 8.1.1 Using Gii................................. 135 8.1.2 Extending Gii............................... 137 8.2 URL Management................................ 143 8.2.1 Creating URLs.............................. 143 8.2.2 User-friendly URLs............................ 144 8.3 Authentication and Authorization....................... 149 8.3.1 Defining Identity Class.......................... 149 8.3.2 Login and Logout............................ 151 8.3.3 Access Control Filter........................... 151 8.3.4 Role-Based Access Control....................... 154 8.4 Theming...................................... 160 8.4.1 Skin.................................... 162 8.5 Logging...................................... 164 8.5.1 Message Logging............................