| 1 |
== Welcome to Trax |
|---|
| 2 |
|
|---|
| 3 |
Trax is a web-application and persistance framework that includes everything |
|---|
| 4 |
needed to create database-backed web-applications according to the |
|---|
| 5 |
Model-View-Control pattern of separation. This pattern splits the view (also |
|---|
| 6 |
called the presentation) into "dumb" templates that are primarily responsible |
|---|
| 7 |
for inserting pre-build data in between HTML tags. The model contains the |
|---|
| 8 |
"smart" domain objects (such as Account, Product, Person, Post) that holds all |
|---|
| 9 |
the business logic and knows how to persist themselves to a database. The |
|---|
| 10 |
controller handles the incoming requests (such as Save New Account, Update |
|---|
| 11 |
Product, Show Post) by manipulating the model and directing data to the view. |
|---|
| 12 |
|
|---|
| 13 |
In Trax, the model is handled by what's called a object-relational mapping |
|---|
| 14 |
layer entitled Active Record. This layer allows you to present the data from |
|---|
| 15 |
database rows as objects and embellish these data objects with business logic |
|---|
| 16 |
methods. |
|---|
| 17 |
|
|---|
| 18 |
The controller and view is handled by the Action Controller |
|---|
| 19 |
|
|---|
| 20 |
== Requirements |
|---|
| 21 |
|
|---|
| 22 |
* Database (MySQL, PostgreSQL, etc) (uses Pear::DB) |
|---|
| 23 |
* PHP 5.x |
|---|
| 24 |
* PEAR::DB, PEAR::Mail / PEAR::Mail_mime |
|---|
| 25 |
* Apache 1.3.x or 2.x |
|---|
| 26 |
|
|---|
| 27 |
== Getting Started |
|---|
| 28 |
|
|---|
| 29 |
1. edit config/environment.php |
|---|
| 30 |
set if its development/test/production environment |
|---|
| 31 |
2. edit config/database.ini |
|---|
| 32 |
set database parameters |
|---|
| 33 |
3. edit public_html/.htaccess |
|---|
| 34 |
set the path to your pear installation (/usr/local/lib/php - on my box) |
|---|
| 35 |
set the path to the config dir for trax (/home/<username>/trax/config) |
|---|
| 36 |
example: .htaccess - php_value include_path .:/usr/local/lib/php:/home/<username>/trax/config |
|---|
| 37 |
4. create your database and use script/generator.php to |
|---|
| 38 |
generate model, controller, and view files |
|---|
| 39 |
./generate.php model user (singular names must be used) |
|---|
| 40 |
./generate.php controller [controller name] [view1 view2 view3 ...] |
|---|
| 41 |
5. main site layouts should be in app/views/layouts |
|---|
| 42 |
default: public.phtml |
|---|
| 43 |
content from view files are displayed in the layout phtml file |
|---|
| 44 |
by using the variable <?= $content_for_layout ?> |
|---|
| 45 |
|
|---|
| 46 |
== Naming Schema |
|---|
| 47 |
|
|---|
| 48 |
* controller names should be Camelized (MyClass). |
|---|
| 49 |
* models should be use singular names such as customer not customers. |
|---|
| 50 |
* table names should always be plural. |
|---|
| 51 |
* foriegn keys should always be singular with _id on the end. |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
== ActiveRecord |
|---|
| 55 |
|
|---|
| 56 |
*Table Relationships |
|---|
| 57 |
|
|---|
| 58 |
In each model class you can define table relationships. |
|---|
| 59 |
Allowed relationships in model classes are: |
|---|
| 60 |
* belongs_to: means that there is a foreign key from another table in in this table. |
|---|
| 61 |
public $belongs_to = array("foreign_table_name" => null); |
|---|
| 62 |
- foreign table name is always singular. |
|---|
| 63 |
* has_one: means that there is a foreign key in another table to this table |
|---|
| 64 |
public $belongs_to = array("foreign_table_name" => null); |
|---|
| 65 |
- foreign table name is always singular. |
|---|
| 66 |
* has_many: means that there is a foreign key in another table to this table |
|---|
| 67 |
public $belongs_to = array("foreign_table_name" => null); |
|---|
| 68 |
- foreign table name is always plural. |
|---|
| 69 |
* has_and_belongs_to_many: means there is a join with another table into a third joining table |
|---|
| 70 |
public $belongs_to = array("foreign_table_name" => null); |
|---|
| 71 |
- foreign table name is always plural. |
|---|
| 72 |
- joining table must be named table1_table2 (plural names for both) |
|---|
| 73 |
|
|---|
| 74 |
In place of the null you can specify an array of parameters such as "foreign_key". |
|---|
| 75 |
public $belongs_to = array("table_name" => array("foreign_key" => "otherkey_id")); |
|---|
| 76 |
|
|---|
| 77 |
If the models name isn't the singular name of the corresponding table it goes with, you can in |
|---|
| 78 |
the model specify the table name. (public $table_name = "different_name";) |
|---|
| 79 |
|
|---|
| 80 |
* Functions |
|---|
| 81 |
|
|---|
| 82 |
* find_all([conditions], [orderings], [limit], [joins]) |
|---|
| 83 |
$array_of_objects = $model->find_all(); // everything |
|---|
| 84 |
$array_of_objects = $model->find_all("id > 10"); // everyone with id > 10 |
|---|
| 85 |
|
|---|
| 86 |
* find(id, [conditions]) |
|---|
| 87 |
$object = $model->find($id); // one object where id=$id |
|---|
| 88 |
|
|---|
| 89 |
* find_first([conditions]) |
|---|
| 90 |
$object = $model->find_first("last_name = 'Smith'"); // one object , first row in result set where last_name='Smith' |
|---|
| 91 |
|
|---|
| 92 |
* find_all_by_[param1]_or|and_[param2]...(param1,param2,etc...,[ordering]) |
|---|
| 93 |
$array_of_objects = $model->find_all_by_lastname_and_firstname_or_last_name("Smith","John","Jones","last_name DESC"); // everything |
|---|
| 94 |
- lastname and firstname are fields in the table. |
|---|
| 95 |
- the function params are the matching order for the fields in the function name |
|---|
| 96 |
- optional last param is the ordering. |
|---|
| 97 |
|
|---|
| 98 |
for aggregrations you can do the following: ("count","sum","avg","max","min") |
|---|
| 99 |
$model->count_all("field",[conditions]); (sum_all, avg_all, etc) |
|---|
| 100 |
|
|---|
| 101 |
for auto updating datetime fields there are 2 arrays defined. Any table with fields with these names |
|---|
| 102 |
will be auto updated when inserting or updating. If you want to turn off auto updating you can set the |
|---|
| 103 |
class var $auto_timestamps = false; |
|---|
| 104 |
protected $auto_update_timestamps = array("updated_at","updated_on"); |
|---|
| 105 |
protected $auto_create_timestamps = array("created_at","created_on"); |
|---|
| 106 |
|
|---|
| 107 |
== Notes |
|---|
| 108 |
|
|---|
| 109 |
Any thing set as a class variable in the controller with become a regular variable in the view file. |
|---|
| 110 |
example: ($this->users becomes $users in the view file as with any class ($this->xxx) variable) |
|---|
| 111 |
url: /browse/show_all |
|---|
| 112 |
|
|---|
| 113 |
controller (browse_controller.php): |
|---|
| 114 |
function show_all() { |
|---|
| 115 |
$user = new User(); |
|---|
| 116 |
$this->users = user->find_all(); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
view file (show_all.phtml): |
|---|
| 120 |
<? if(count($users) > 0): ?> |
|---|
| 121 |
<? foreach($users as $user): ?> |
|---|
| 122 |
<?=$user->first_name?> <?=$user->last_name?><br> |
|---|
| 123 |
<? endforeach; ?> |
|---|
| 124 |
<? endif; ?> |
|---|
| 125 |
|
|---|
| 126 |
view results: |
|---|
| 127 |
john smith |
|---|
| 128 |
joe smith |
|---|
| 129 |
larry king |
|---|
| 130 |
... |
|---|
| 131 |
|
|---|
| 132 |
== Description of contents |
|---|
| 133 |
|
|---|
| 134 |
trax/app |
|---|
| 135 |
Holds all the code that's specific to this particular application. |
|---|
| 136 |
|
|---|
| 137 |
trax/app/controllers |
|---|
| 138 |
Holds controllers that should be named like weblog_controller.php for |
|---|
| 139 |
automated URL mapping. All controllers should descend from |
|---|
| 140 |
ActionController. |
|---|
| 141 |
|
|---|
| 142 |
trax/app/models |
|---|
| 143 |
Holds models that should be named like post.rb. |
|---|
| 144 |
Most models will descent from ActiveRecord. |
|---|
| 145 |
|
|---|
| 146 |
trax/app/views |
|---|
| 147 |
Holds the template files for the view that should be named like |
|---|
| 148 |
weblog/index.phtml for the WeblogController->index action. |
|---|
| 149 |
|
|---|
| 150 |
trax/app/helpers |
|---|
| 151 |
Holds view helpers that should be named like weblog_helper.php. |
|---|
| 152 |
|
|---|
| 153 |
trax/config |
|---|
| 154 |
Configuration files for the Trax environment, the routing map, the database, and other dependencies. |
|---|
| 155 |
|
|---|
| 156 |
trax/lib |
|---|
| 157 |
Application specific libraries. Basically, any kind of custom code that doesn't |
|---|
| 158 |
belong controllers, models, or helpers. This directory is in the includes path. |
|---|
| 159 |
|
|---|
| 160 |
trax/script |
|---|
| 161 |
Helper scripts for automation and generation. |
|---|
| 162 |
|
|---|
| 163 |
public / public_html |
|---|
| 164 |
The directory available for the web server. Contains sub-directories for images, stylesheets, |
|---|
| 165 |
and javascripts. Also contains the .htaccess, dispatcher, and default HTML files. |
|---|
| 166 |
|
|---|