From 55a7cf604095576ab87cb21c62bd27632d63d2e4 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Fri, 30 Aug 2013 10:47:11 +1200 Subject: [PATCH] Documentation for belongs_to --- docs/en/topics/datamodel.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/en/topics/datamodel.md b/docs/en/topics/datamodel.md index 67494760c..9a85dfe72 100755 --- a/docs/en/topics/datamodel.md +++ b/docs/en/topics/datamodel.md @@ -584,6 +584,35 @@ object type. } +### belongs_to + +Defines a 1-to-1 relationship with another object, which declares the other end +of the relationship with a corresponding $has_one. A single database column named +`ID` will be created in the object with the $has_one, but +the $belongs_to by itself will not create a database field. This field will hold +the ID of the object declaring the $belongs_to. + +Similarly with $has_many, dot notation can be used to explicitly specify the $has_one +which refers to this relation. This is not mandatory unless the relationship would +be otherwise ambiguous. + + :::php + + class Torso extends DataObject { + // HeadID will be generated on the Torso table + private static $has_one = array( + 'Head' => 'Head' + ); + } + + class Head extends DataObject { + // No database field created. The '.Head' suffix could be omitted + private static $belongs_to = array( + 'Torso' => 'Torso.Head' + ); + } + + ### many_many Defines many-to-many joins. A new table, (this-class)_(relationship-name), will