Changeset 366
- Timestamp:
- 09/19/07 05:04:28 (1 year ago)
- Files:
-
- trunk/app/models/framework_setup.php (modified) (1 diff)
- trunk/docs/tutorial.markdown (modified) (1 diff)
- trunk/lib/AkActiveRecord/AkObserver.php (modified) (1 diff)
- trunk/lib/constants.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/app/models/framework_setup.php
r365 r366 723 723 } 724 724 } 725 726 725 } 727 726 trunk/docs/tutorial.markdown
r322 r366 258 258 ./script/generate scaffold Author 259 259 260 This will generate a bunch of files and folders with code that really works!. Don't trust me? Try it yourself. Point your browser to [http://localhost/booklink/author](http://localhost/booklink/author) and [http://localhost/booklink/books](http://localhost/booklink/books) to start adding authors and books. Create some records and come back for an explanation of what is going under the hood.260 This will generate a bunch of files and folders with code that really works!. Don't trust me? Try it yourself. Point your browser to [http://localhost/booklink/author](http://localhost/booklink/author) and [http://localhost/booklink/books](http://localhost/booklink/books) or to [http://localhost/booklink/?ak=author](http://localhost/booklink/?ak=author) and [http://localhost/booklink/?ak=books](http://localhost/booklink/?ak=books) in case mod_rewrite is disabled to start adding authors and books. Create some records and come back for an explanation of what is going under the hood. 261 261 262 262 trunk/lib/AkActiveRecord/AkObserver.php
r285 r366 22 22 23 23 /** 24 * Observer classes respond to lifecycle callbacks to implement trigger-like 25 * behavior outside the original class. This is a great way to reduce the 26 * clutter that normally comes when the model class is burdened with 27 * functionality that doesn't pertain to the core responsibility of the 28 * class. Example: 24 * Observer classes respond to life-cycle callbacks to implement trigger-like 25 * behavior outside the original class. This is a great way to reduce the clutter 26 * that normally comes when the model class is burdened with functionality that 27 * doesn't pertain to the core responsibility of the class. 29 28 * 30 * class CommentObserver extends AkObserver 31 * { 32 * function afterSave($comment) 33 * { 34 * Ak::mail("admin@example.com", "New comment was posted", $comment->toString()); 35 * } 36 * } 29 * Example: 30 * 31 * class CommentObserver extends AkObserver 32 * { 33 * function afterSave($comment) 34 * { 35 * Ak::mail("admin@example.com", "New comment was posted", 36 * $comment->toString()); 37 * } 38 * } 37 39 * 38 40 * This Observer sends an email when a Comment::save is finished. 39 41 * 40 * ==Observing a class that can't be inferred42 * ## Observing a class that can't be inferred 41 43 * 42 * Observers will by default be mapped to the class with which they share a name. So CommentObserver will 43 * be tied to observing Comment, ProductManagerObserver to ProductManager, and so on. If you want to name your observer 44 * differently than the class you're interested in observing, you can use the AkActiveRecord->observe() class method: 44 * Observers will by default be mapped to the class with which they share a name. 45 * So CommentObserver will be tied to observing Comment, ProductManagerObserver 46 * to ProductManager, and so on. If you want to name your observer differently 47 * than the class you're interested in observing, you can use the 48 * AkActiveRecord->observe() class method: 45 49 * 46 * class AuditObserver extends AkObserver 47 * { 48 * function __construct() 49 * { 50 * $this->observe('Account'); 51 * } 50 * function afterUpdate(&$account) 51 * { 52 * $AuditTrail =& new AuditTrail($account, "UPDATED"); 53 * $AuditTrail->save(); 54 * } 52 55 * 53 * function afterUpdate(&$account) 54 * { 55 * $AuditTrail =& new AuditTrail($account, "UPDATED"); 56 * $AuditTrail->save(); 57 * } 58 * } 56 * If the audit observer needs to watch more than one kind of object, this can be 57 * specified with multiple arguments: 59 58 * 60 * If the audit observer needs to watch more than one kind of object, this can be specified with multiple arguments: 59 * function afterUpdate(&$record) 60 * { 61 * $ObservedRecord =& new AuditTrail($record, "UPDATED"); 62 * $ObservedRecord->save(); 63 * } 61 64 * 62 * class AuditObserver extends AkObserver 63 * { 64 * function __construct() 65 * { 66 * $this->observe('Account', 'Balance'); 67 * } 65 * The AuditObserver will now act on both updates to Account and Balance by 66 * treating them both as records. 68 67 * 69 * function afterUpdate(&$record) 70 * { 71 * $ObservedRecord =& new AuditTrail($record, "UPDATED"); 72 * $ObservedRecord->save(); 73 * } 74 * } 68 * ## Available callback methods 75 69 * 76 * The AuditObserver will now act on both updates to Account and Balance by treating them both as records. 70 * The observer can implement callback methods for each of these methods: 71 * beforeCreate, beforeValidation, beforeValidationOnCreate, beforeSave, 72 * afterValidation, afterValidationOnCreate, afterCreate and afterSave 77 73 * 78 * == Available callback methods74 * ## Triggering Observers 79 75 * 80 * The observer can implement callback methods for each of these methods: beforeCreate, beforeValidation, beforeValidationOnCreate, beforeSave, afterValidation, afterValidationOnCreate, afterCreate and afterSave 76 * In order to activate an observer, you need to call create an Observer instance 77 * and attach it to a model. 81 78 * 82 * == Triggering Observers 79 * In the Akelos Framework, this can be done in controllers using the short-hand 80 * of for example: 83 81 * 84 * In order to activate an observer, you need to call create an Observer instance and attach it to a model. 85 * In the Akelos Framework, this can be done in controllers 86 * using the short-hand of for example 87 * $ComentObserverInstance =& new CommentObserver(); 88 * $Model->addObserver(&$ComentObserverInstance); 82 * $ComentObserverInstance =& new CommentObserver(); 83 * $Model->addObserver(&$ComentObserverInstance); 84 * 89 85 */ 90 91 86 class AkObserver extends AkObject 92 87 { trunk/lib/constants.php
r365 r366 115 115 if(!AK_CLI && AK_WEB_REQUEST){ 116 116 117 defined('AK_SITE_URL_SUFFIX') ? null : define('AK_SITE_URL_SUFFIX', str_replace(array(join(DS,array_diff((array)@explode(DS,AK_BASE_DIR), (array)@explode('/',AK_REQUEST_URI))), DS ), array('','/'), AK_BASE_DIR));117 defined('AK_SITE_URL_SUFFIX') ? null : define('AK_SITE_URL_SUFFIX', str_replace(array(join(DS,array_diff((array)@explode(DS,AK_BASE_DIR), (array)@explode('/',AK_REQUEST_URI))), DS,'//'), array('','/','/'), AK_BASE_DIR)); 118 118 119 119 defined('AK_AUTOMATIC_SSL_DETECTION') ? null : define('AK_AUTOMATIC_SSL_DETECTION', 1);
