You are viewing documentation for v1.1.x. Change

hasOne()

Description

Sets up a hasOne association between this model and the specified one.

Function Syntax

hasOne(name [, modelName, foreignKey, joinType, dependent ])

Parameters

Parameter 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 No Name of associated model (usually not needed if you follow Wheels conventions because the model name will be deduced from the name argument).
foreignKey string No Foreign key property name (usually not needed if you follow Wheels conventions since the foreign key name will be deduced from the name argument).
joinType string No outer Use to set the join type when joining associated tables. Possible values are inner (for INNER JOIN) and outer (for LEFT OUTER JOIN).
dependent string No false Defines how to handle dependent models when you delete a record from this model. Set to delete to instantiate associated models and call their delete() method, deleteAll to delete without instantiating, nullify to remove the foreign key, or false to do nothing.

Examples

<!--- Specify that instances of this model has one profile. (The table for the associated model, not the current, should have the foreign key set on it.) --->
<cfset hasOne("profile")>

<!--- Same as above but setting the `joinType` to `inner`, which basically means this model should always have a record in the `profiles` table --->
<cfset hasOne(name="profile", joinType="inner")>

<!--- Automatically delete the associated `profile` whenever this object is deleted --->
<cfset hasMany(name="comments", dependent="delete")>