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-2013. All Rights Reserved. Contents Contents i License xiii 1 Getting Started1 1.1 The Definitive Guide to Yii...........................1 1.2 New Features...................................1 1.2.1 Version 1.1.15...............................1 1.2.2 Version 1.1.14...............................1 1.2.3 Version 1.1.11...............................1 1.2.4 Version 1.1.8...............................2 1.2.5 Version 1.1.7...............................2 1.2.6 Version 1.1.6...............................2 1.2.7 Version 1.1.5...............................2 1.2.8 Version 1.1.4...............................2 1.2.9 Version 1.1.3...............................3 1.2.10 Version 1.1.2...............................3 1.2.11 Version 1.1.1...............................3 1.2.12 Version 1.1.0...............................3 1.3 Upgrading from Version 1.0 to 1.1.......................4 1.3.1 Changes Related with Model Scenarios.................4 ii Contents 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...4 1.3.4 Changes Related with Tabular Input..................5 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 does Yii Compare with Other Frameworks?...........5 1.5 Installation....................................6 1.5.1 Requirements...............................6 1.6 Apache and Nginx configurations........................6 1.6.1 Apache..................................6 1.6.2 Nginx...................................7 1.7 Creating Your First Yii Application......................8 1.7.1 Connecting to Database......................... 13 1.7.2 Implementing CRUD Operations.................... 14 2 Fundamentals 19 2.1 Model-View-Controller (MVC)......................... 19 2.1.1 A Typical Workflow........................... 20 2.2 Entry Script.................................... 21 2.2.1 Debug Mode............................... 21 2.3 Application.................................... 22 2.3.1 Application Configuration........................ 22 2.3.2 Application Base Directory....................... 23 Contents iii 2.3.3 Application Components......................... 23 2.3.4 Core Application Components..................... 24 2.3.5 Application Life Cycle.......................... 25 2.4 Controller..................................... 26 2.4.1 Route................................... 27 2.4.2 Controller Instantiation......................... 27 2.4.3 Action................................... 28 2.4.4 Filter................................... 31 2.5 Model....................................... 33 2.6 View........................................ 33 2.6.1 Layout................................... 34 2.6.2 Widget.................................. 34 2.6.3 System View............................... 36 2.7 Component.................................... 36 2.7.1 Defining and using a Component Property............... 36 2.7.2 Component Event............................ 38 2.7.3 Component Behavior........................... 39 2.8 Module...................................... 40 2.8.1 Creating Module............................. 41 2.8.2 Using Module............................... 41 2.8.3 Nested Module.............................. 42 2.9 Path Alias and Namespace........................... 43 2.9.1 Root Alias................................ 43 2.9.2 Importing Classes............................ 43 iv Contents 2.9.3 Importing Directories.......................... 44 2.9.4 Namespace................................ 44 2.9.5 Namespaced Classes........................... 45 2.9.6 Namespaced Controllers......................... 45 2.9.7 Namespaced Modules.......................... 47 2.10 Conventions.................................... 48 2.10.1 URL.................................... 48 2.10.2 Code.................................... 49 2.10.3 Configuration............................... 49 2.10.4 File.................................... 49 2.10.5 Directory................................. 50 2.10.6 Database................................. 51 2.11 Development Workflow.............................. 51 2.12 Best MVC Practices............................... 52 2.12.1 Model................................... 53 2.12.2 View.................................... 54 2.12.3 Controller................................. 55 3 Working with Forms 57 3.1 Working with Form................................ 57 3.2 Creating Model.................................. 57 3.2.1 Defining Model Class........................... 58 3.2.2 Declaring Validation Rules....................... 58 3.2.3 Securing Attribute Assignments..................... 61 3.2.4 Triggering Validation........................... 63 Contents v 3.2.5 Retrieving Validation Errors...................... 64 3.2.6 Attribute Labels............................. 64 3.3 Creating Action.................................. 65 3.4 Creating Form.................................. 66 3.5 Collecting Tabular Input............................. 68 3.6 Using Form Builder............................... 70 3.6.1 Basic Concepts.............................. 70 3.6.2 Creating a Simple Form......................... 70 3.6.3 Specifying Form Elements........................ 72 3.6.4 Accessing Form Elements........................ 76 3.6.5 Creating a Nested Form......................... 77 3.6.6 Customizing Form Display....................... 79 4 Working with Databases 81 4.1 Working with Database............................. 81 4.2 Data Access Objects (DAO)........................... 81 4.2.1 Establishing Database Connection................... 82 4.2.2 Using ODBC Connection........................ 83 4.2.3 Executing SQL Statements....................... 84 4.2.4 Fetching Query Results......................... 84 4.2.5 Using Transactions............................ 85 4.2.6 Binding Parameters........................... 86 4.2.7 Binding Columns............................. 86 4.2.8 Using Table Prefix............................ 87 4.3 Query Builder................................... 87 vi Contents 4.3.1 Preparing Query Builder......................... 88 4.3.2 Building Data Retrieval Queries.................... 88 4.3.3 Building Data Manipulation Queries.................. 97 4.3.4 Building Schema Manipulation Queries................ 99 4.4 Active Record................................... 105 4.4.1 Establishing DB Connection....................... 106 4.4.2 Defining AR Class............................ 107 4.4.3 Creating Record............................. 109 4.4.4 Reading Record.............................. 110 4.4.5 Updating Record............................. 112 4.4.6 Deleting Record............................. 113 4.4.7 Data Validation.............................. 114 4.4.8 Comparing Records........................... 114 4.4.9 Customization.............................. 115 4.4.10 Using Transaction with AR....................... 115 4.4.11 Named Scopes.............................. 116 4.5 Relational Active Record............................ 118 4.5.1 Declaring Relationship.......................... 119 4.5.2 Performing Relational Query...................... 121 4.5.3 Performing Relational query without getting related models..... 123 4.5.4 Relational Query Options........................ 123 4.5.5 Disambiguating Column Names..................... 125 4.5.6 Dynamic Relational Query Options................... 126 4.5.7 Relational Query Performance..................... 127 Contents vii 4.5.8 Statistical Query............................. 128 4.5.9 Relational Query with Named Scopes................. 130 4.5.10 Relational Query with through..................... 132 4.5.11 Post-JOIN operations.......................... 135 4.6 Database Migration............................... 138 4.6.1 Creating Migrations........................... 139 4.6.2 Transactional Migrations........................ 141 4.6.3 Applying Migrations........................... 142 4.6.4 Reverting Migrations........................... 143 4.6.5 Redoing Migrations........................... 143 4.6.6 Showing Migration Information..................... 144 4.6.7 Modifying Migration History...................... 144 4.6.8 Customizing Migration Command................... 144 5 Caching 147 5.1 Caching...................................... 147 5.2 Data Caching................................... 149 5.2.1 Cache Dependency............................ 150 5.2.2 Query Caching.............................. 151 5.3 Fragment Caching................................ 153 5.3.1 Caching Options............................. 154 5.3.2 Nested Caching.............................. 156 5.4 Page Caching................................... 157 5.4.1 Output Caching............................. 157 5.4.2 HTTP Caching.............................. 157 viii Contents 5.5 Dynamic Content................................. 159 6 Extending Yii 161 6.1 Extending Yii................................... 161 6.2 Using Extensions................................. 162 6.2.1 Zii Extensions............................... 162 6.2.2 Application Component......................... 162 6.2.3 Behavior.................................. 163 6.2.4 Widget.................................. 164 6.2.5 Action................................... 165 6.2.6 Filter................................... 165 6.2.7