| 
						
						
							
								
							
						
						
					 | 
					 | 
					@ -133,10 +133,10 @@ $values = $customer->attributes; | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					``` | 
					 | 
					 | 
					 | 
					``` | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Persisting Data to Database | 
					 | 
					 | 
					 | 
					Saving Data to Database | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					--------------------------- | 
					 | 
					 | 
					 | 
					----------------------- | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					ActiveRecord provides the following methods to insert, update and delete data: | 
					 | 
					 | 
					 | 
					ActiveRecord provides the following methods to insert, update and delete data in the database: | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					- [[save()]] | 
					 | 
					 | 
					 | 
					- [[save()]] | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					- [[insert()]] | 
					 | 
					 | 
					 | 
					- [[insert()]] | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -207,9 +207,9 @@ a one-many relationship. For example, a customer has many orders. And the [[hasO | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					method declares a many-one or one-one relationship. For example, an order has one customer. | 
					 | 
					 | 
					 | 
					method declares a many-one or one-one relationship. For example, an order has one customer. | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Both methods take two parameters: | 
					 | 
					 | 
					 | 
					Both methods take two parameters: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					- `$class`: the name of the class related models should use. If specified without | 
					 | 
					 | 
					 | 
					- `$class`: the name of the class of the related model(s). If specified without | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  a namespace, the namespace will be taken from the declaring class. | 
					 | 
					 | 
					 | 
					  a namespace, the namespace of the related model class will be taken from the declaring class. | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					- `$link`: the association between columns from two tables. This should be given as an array. | 
					 | 
					 | 
					 | 
					- `$link`: the association between columns from the two tables. This should be given as an array. | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  The keys of the array are the names of the columns from the table associated with `$class`, | 
					 | 
					 | 
					 | 
					  The keys of the array are the names of the columns from the table associated with `$class`, | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  while the values of the array are the names of the columns from the declaring class. | 
					 | 
					 | 
					 | 
					  while the values of the array are the names of the columns from the declaring class. | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  It is a good practice to define relationships based on table foreign keys. | 
					 | 
					 | 
					 | 
					  It is a good practice to define relationships based on table foreign keys. | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -263,8 +263,8 @@ class Order extends \yii\db\ActiveRecord | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					``` | 
					 | 
					 | 
					 | 
					``` | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					[[ActiveRelation::via()]] method is similar to [[ActiveRelation::viaTable()]] except that | 
					 | 
					 | 
					 | 
					[[ActiveRelation::via()]] method is similar to [[ActiveRelation::viaTable()]] except that | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					the first parameter of [[ActiveRelation::via()]] takes a relation name declared in the ActiveRecord class. | 
					 | 
					 | 
					 | 
					the first parameter of [[ActiveRelation::via()]] takes a relation name declared in the ActiveRecord class | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					For example, the above `items` relation can be equivalently declared as follows: | 
					 | 
					 | 
					 | 
					instead of the pivot table name. For example, the above `items` relation can be equivalently declared as follows: | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					```php | 
					 | 
					 | 
					 | 
					```php | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					class Order extends \yii\db\ActiveRecord | 
					 | 
					 | 
					 | 
					class Order extends \yii\db\ActiveRecord | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -315,7 +315,7 @@ How many SQL queries will be performed in the above code, assuming there are mor | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					the database? 101! The first SQL query brings back 100 customers. Then for each customer, a SQL query | 
					 | 
					 | 
					 | 
					the database? 101! The first SQL query brings back 100 customers. Then for each customer, a SQL query | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					is performed to bring back the customer's orders. | 
					 | 
					 | 
					 | 
					is performed to bring back the customer's orders. | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					To solve the above performance problem, you can use the so-called *eager loading* by calling [[ActiveQuery::with()]]: | 
					 | 
					 | 
					 | 
					To solve the above performance problem, you can use the so-called *eager loading* approach by calling [[ActiveQuery::with()]]: | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					```php | 
					 | 
					 | 
					 | 
					```php | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					// SQL executed: SELECT * FROM tbl_customer LIMIT 100 | 
					 | 
					 | 
					 | 
					// SQL executed: SELECT * FROM tbl_customer LIMIT 100 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -473,7 +473,7 @@ TODO: FIXME: WIP, TBD, https://github.com/yiisoft/yii2/issues/226 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Imagine situation where you have to save something related to the main model in [[beforeSave()]], | 
					 | 
					 | 
					 | 
					Imagine situation where you have to save something related to the main model in [[beforeSave()]], | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					[[afterSave()]], [[beforeDelete()]] and/or [[afterDelete()]] life cycle methods. Developer may come | 
					 | 
					 | 
					 | 
					[[afterSave()]], [[beforeDelete()]] and/or [[afterDelete()]] life cycle methods. Developer may come | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					to solution of overriding ActiveRecord [[save()]] method with database transaction wrapping or | 
					 | 
					 | 
					 | 
					to the solution of overriding ActiveRecord [[save()]] method with database transaction wrapping or | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					even using transaction in controller action, which is strictly speaking doesn't seems to be a good | 
					 | 
					 | 
					 | 
					even using transaction in controller action, which is strictly speaking doesn't seems to be a good | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					practice (recall skinny-controller fat-model fundamental rule). | 
					 | 
					 | 
					 | 
					practice (recall skinny-controller fat-model fundamental rule). | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
					 | 
					
  |