| @@ 1053-1067 (lines=15) @@ | ||
| 1050 | * |
|
| 1051 | * @return Relation |
|
| 1052 | */ |
|
| 1053 | public function hasOne($model, $foreignKey = '', $localKey = '') |
|
| 1054 | { |
|
| 1055 | // the default local key would look like `user_id` |
|
| 1056 | // for a model named User |
|
| 1057 | if (!$foreignKey) { |
|
| 1058 | $inflector = Inflector::get(); |
|
| 1059 | $foreignKey = strtolower($inflector->underscore(static::modelName())).'_id'; |
|
| 1060 | } |
|
| 1061 | ||
| 1062 | if (!$localKey) { |
|
| 1063 | $localKey = self::DEFAULT_ID_PROPERTY; |
|
| 1064 | } |
|
| 1065 | ||
| 1066 | return new HasOne($model, $foreignKey, $localKey, $this); |
|
| 1067 | } |
|
| 1068 | ||
| 1069 | /** |
|
| 1070 | * Creates the child side of a One-To-One or One-To-Many relationship. |
|
| @@ 1078-1092 (lines=15) @@ | ||
| 1075 | * |
|
| 1076 | * @return Relation |
|
| 1077 | */ |
|
| 1078 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
|
| 1079 | { |
|
| 1080 | if (!$foreignKey) { |
|
| 1081 | $foreignKey = self::DEFAULT_ID_PROPERTY; |
|
| 1082 | } |
|
| 1083 | ||
| 1084 | // the default local key would look like `user_id` |
|
| 1085 | // for a model named User |
|
| 1086 | if (!$localKey) { |
|
| 1087 | $inflector = Inflector::get(); |
|
| 1088 | $localKey = strtolower($inflector->underscore($model::modelName())).'_id'; |
|
| 1089 | } |
|
| 1090 | ||
| 1091 | return new BelongsTo($model, $foreignKey, $localKey, $this); |
|
| 1092 | } |
|
| 1093 | ||
| 1094 | /** |
|
| 1095 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
|
| @@ 1103-1117 (lines=15) @@ | ||
| 1100 | * |
|
| 1101 | * @return Relation |
|
| 1102 | */ |
|
| 1103 | public function hasMany($model, $foreignKey = '', $localKey = '') |
|
| 1104 | { |
|
| 1105 | // the default local key would look like `user_id` |
|
| 1106 | // for a model named User |
|
| 1107 | if (!$foreignKey) { |
|
| 1108 | $inflector = Inflector::get(); |
|
| 1109 | $foreignKey = strtolower($inflector->underscore(static::modelName())).'_id'; |
|
| 1110 | } |
|
| 1111 | ||
| 1112 | if (!$localKey) { |
|
| 1113 | $localKey = self::DEFAULT_ID_PROPERTY; |
|
| 1114 | } |
|
| 1115 | ||
| 1116 | return new HasMany($model, $foreignKey, $localKey, $this); |
|
| 1117 | } |
|
| 1118 | ||
| 1119 | /** |
|
| 1120 | * Creates the child side of a Many-To-Many relationship. |
|
| @@ 1128-1142 (lines=15) @@ | ||
| 1125 | * |
|
| 1126 | * @return Relation |
|
| 1127 | */ |
|
| 1128 | public function belongsToMany($model, $foreignKey = '', $localKey = '') |
|
| 1129 | { |
|
| 1130 | if (!$foreignKey) { |
|
| 1131 | $foreignKey = self::DEFAULT_ID_PROPERTY; |
|
| 1132 | } |
|
| 1133 | ||
| 1134 | // the default local key would look like `user_id` |
|
| 1135 | // for a model named User |
|
| 1136 | if (!$localKey) { |
|
| 1137 | $inflector = Inflector::get(); |
|
| 1138 | $localKey = strtolower($inflector->underscore($model::modelName())).'_id'; |
|
| 1139 | } |
|
| 1140 | ||
| 1141 | return new BelongsToMany($model, $foreignKey, $localKey, $this); |
|
| 1142 | } |
|
| 1143 | ||
| 1144 | ///////////////////////////// |
|
| 1145 | // Events |
|