Skip to content

apitabledefinitionreferences

Troy Murray edited this page Sep 15, 2011 · 2 revisions

Table Definitions :: references function

Usage

Adds column definition to the table definition object for an association column (belongsTo, hasOne, hasMany) using the CFWheels conventions. Optionally adds a foreign key constraint.

t.references(referenceNames[,default,null,polymorphic,foreignKey]);

Parameters

Parameter Required Default Description referenceNames Yes one or more reference names (singular of referenced tables, eg. user), comma delimited default No NULL default value for column null No True boolean, whether nulls are allowed polymorphic No False boolean, whether or not to create an Id/Type pair of columns for a polymorphic relationship foreignKey No True boolean, whether or not to create a foreign key constraint, ignored if polymorphic set to True

Examples

t.references('user');

will create a column called userid and a foreign key constraint to the id field in the users table.

t.references(referenceNames='user',foreignKey=false);

will create a column called userid without creating a foreign key constraint.

t.references(referenceNames='contact',polymorphic=true);

will create a columns for a polymorphic relationship called contactid and contacttype. You could make use of this in CFWheels by making the contacttype refer to a model name, then using model(this.contacttype).findByKey(this.contactid).

Notes

Prior to version 0.4, the convention used for column names was camelCase (e.g. contentId and contentType)

Clone this wiki locally