In Recess Models use a variation on the ActiveRecord pattern to provide an Object-Relational Mapping facility in the Recess Framework.
The assumed table name is the
Model's class name in lowercase
. To override this add a
!Table [table_name]
annotation to your model. When providing your own name for the table
be sure to follow the case-sensitivity expectations of your RDBMS & OS MySql.
Example 13.5. Default naming conventions for Model Class -> Table
<?php class Post extends Model {} // Table: post class Person extends Model {} // Table: person
Example 13.6. Overriding the default naming convention of Model Class -> Table
<?php /** !Table posts */ class Post extends Model {} // Table: posts /** !Table people */ class Person extends Model {} // Table: people
Example 13.7. Example naming convention for !BelongsTo
<?php /** !BelongsTo person */ class Post extends Model {} class Person extends Model {} $post->person(); // querying the relationship
Table 13.2. Naming conventions for !BelongsTo
| For | Convention | Example | Override |
|---|---|---|---|
| Related Class | ucfirst( BelongsToName ) | Person | Class: ClassName |
| Foreign Key | BelongsToName . 'Id' | personId | Key: ColumnName |
Example 13.8. Example overriding !BelongsTo conventions
<?php /** !BelongsTo author, Class: Person, Key: personId */ class Post extends Model {} class Person extends Model {} // usage: $post->author();
Example 13.9. Example naming convention for !HasMany
<?php /** !BelongsTo author, Class: Person, Key: personId */ class Post extends Model {} /** !HasMany post */ class Person extends Model {} $person->post(); // usage
Table 13.4. Naming conventions for !HasMany
| For | Convention | Example | Override |
|---|---|---|---|
| Related Class | ucfirst( HasManyName ) | Post | Class: ClassName |
| Foreign Key | lcfirst( Class ) . 'Id' | personId | Key: ColumnName |
Example 13.10. Example overiding naming convention for !HasMany
<?php /** !BelongsTo author, Class: Person, Key: personId */ class Post extends Model {} /** !HasMany posts, Class: Post, To: author */ class Person extends Model {} // usage: $author->posts();
Example 13.11. Example naming conventions for !HasMany, Through
<?php /** * !BelongsTo author, Class: Person, Key: personId * !HasMany tag, Through: TagsPosts **/ class Post extends Model {} /** !HasMany post, Through: TagsPosts */ class Tag extends Model {} /** !BelongsTo post */ /** !BelongsTo tag */ class TagsPosts extends Model {} // usage: $post->tag();
Table 13.5. Variables used in following table
| Variable | Value |
|---|---|
| HasManyName | tag |
| Class | Post |
| ThroughClass | TagsPosts |
The following "From" and "To" are a planned convention not yet supported.
Table 13.6. Naming conventions for !HasMany, Through
| For | Convention | Example | Override |
|---|---|---|---|
| Related Class | ucfirst( HasManyName ) | Tag | Class: ClassName |
| Through Class | ThroughClass | TagsPosts | Through: ThroughClass |
| Through's From Rltn | lcfirst( Class ) | post | Through: ClassName, From: localRelation |
| Through's To Rltn | HasManyName | tag | Through: Classname, To: foreignRelation |
Example 13.12. Example of overriding naming conventions for !HasMany, Through
<?php /** !HasMany tags, Through: TagsPosts, From: postRltn, To: tagRltn */ class Post extends Model {} /** !HasMany posts, Through: TagsPosts, From: tagRltn, To: postRltn */ class Tag extends Model {} /** !BelongsTo postRltn, Class: Post, Key: post_id */ /** !BelongsTo tagRltn, Class: Tag, Key: tag_id */ class TagsPosts extends Model { } // usage: $post->tags();