Skip to content

Model Naming Conventions

KrisJordan edited this page Sep 13, 2010 · 10 revisions

In Recess! Models use a variation on the ActiveRecord pattern to provide an Object-Relational Mapping facility in the Recess Framework.

Model to Table

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 model be sure to follow the case-sensitivity expectations of your RDBMS & OS (MySql).

Example:


class Post extends Model {} // Table: post
class Person extends Model {} // Table: person

Example with overrides:


/** !Table posts */
class Post extends Model {} // Table: posts

/** !Table people */
class Person extends Model {} // Table: people

BelongsTo Relationship

Example:


/** !BelongsTo person */
class Post extends Model {}

class Person extends Model {}

// usage:
$post→person();

Variables used in table:
| Variable | Value |
| BelongsToName | person |

For Convention Example Override
Related Class ucfirst(BelongsToName) Person , Class: ClassName
Foreign Key BelongsToName . ‘Id’ personId , Key: ColumnName

Example using overrides:


/** !BelongsTo author, Class: Person, Key: personId */
class Post extends Model {}

class Person extends Model {}

// usage:
$post→author();

HasMany Relationship

Example:


/** !BelongsTo author, Class: Person, Key: personId */
class Post extends Model {}

/** !HasMany post */
class Person extends Model {}

// usage:
$person→post();

Variables used in table:
| Variable | Value |
| HasManyName | post |
| Class | Person |

For Convention Example Override
Related Class ucfirst(HasManyName) Post , Class: ClassName
Foreign Key lcfirst(Class) . ‘Id’ personId , Key: ColumnName

Example using overrides:


/** !BelongsTo author, Class: Person, Key: personId */
class Post extends Model {}

/** !HasMany posts, Class: Post, To: author */
class Person extends Model {}

// usage:
$author→posts();

HasMany, Through Relationship

Example:


/**
  • !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();

Variables used in table (relative to model Post):
| Variable | Value |
| HasManyName | tag |
| Class | Post |
| ThroughClass | TagsPosts |

For Convention Example Override
Related Class ucfirst(HasManyName) Tag , Class: ClassName
Through Class ThroughClass TagsPosts , Through: ClassName
Through’s From Rltn lcfirst(Class) post , Through: (ClassName, From: localRelation)
Through’s To Rltn HasManyName tag , Through: (Classname, To: foreignRelation)

Example with overrides:


/** !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();