Miscellaneous
belongsTo()
Signature
Section titled “Signature”belongsTo() — returns any
Description
Section titled “Description”Sets up a belongsTo association between this model and the specified one. Use this association when this model contains a foreign key referencing another model.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | yes | — | Gives the association a name that you refer to when working with the association (in the include argument to findAll, to name one example). |
modelName | string | yes | — | Name of associated model (usually not needed if you follow CFWheels conventions because the model name will be deduced from the name argument). |
foreignKey | string | yes | — | Foreign key property name (usually not needed if you follow CFWheels conventions since the foreign key name will be deduced from the name argument). |
joinKey | string | yes | — | Column name to join to if not the primary key (usually not needed if you follow wheels conventions since the join key will be the tables primary key/keys). |
joinType | string | yes | inner | Use to set the join type when joining associated tables. Possible values are inner (for INNER JOIN) and outer (for LEFT OUTER JOIN). |
Examples
Section titled “Examples”// Specify that instances of this model belong to an author. (The table for this model should have a foreign key set on it, typically named `authorid`.)
belongsTo("author");
// Same as above, but because we have broken away from the foreign key naming convention, we need to set `modelName` and `foreignKey`
belongsTo(name="bookWriter", modelName="author", foreignKey="authorId");