| @@ 1144-1158 (lines=15) @@ | ||
| 1141 | * |
|
| 1142 | * @return Relation |
|
| 1143 | */ |
|
| 1144 | public function hasOne($model, $foreignKey = '', $localKey = '') |
|
| 1145 | { |
|
| 1146 | // the default local key would look like `user_id` |
|
| 1147 | // for a model named User |
|
| 1148 | if (!$foreignKey) { |
|
| 1149 | $inflector = Inflector::get(); |
|
| 1150 | $foreignKey = strtolower($inflector->underscore(static::modelName())).'_id'; |
|
| 1151 | } |
|
| 1152 | ||
| 1153 | if (!$localKey) { |
|
| 1154 | $localKey = self::DEFAULT_ID_PROPERTY; |
|
| 1155 | } |
|
| 1156 | ||
| 1157 | return new HasOne($model, $foreignKey, $localKey, $this); |
|
| 1158 | } |
|
| 1159 | ||
| 1160 | /** |
|
| 1161 | * Creates the child side of a One-To-One or One-To-Many relationship. |
|
| @@ 1169-1183 (lines=15) @@ | ||
| 1166 | * |
|
| 1167 | * @return Relation |
|
| 1168 | */ |
|
| 1169 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
|
| 1170 | { |
|
| 1171 | if (!$foreignKey) { |
|
| 1172 | $foreignKey = self::DEFAULT_ID_PROPERTY; |
|
| 1173 | } |
|
| 1174 | ||
| 1175 | // the default local key would look like `user_id` |
|
| 1176 | // for a model named User |
|
| 1177 | if (!$localKey) { |
|
| 1178 | $inflector = Inflector::get(); |
|
| 1179 | $localKey = strtolower($inflector->underscore($model::modelName())).'_id'; |
|
| 1180 | } |
|
| 1181 | ||
| 1182 | return new BelongsTo($model, $foreignKey, $localKey, $this); |
|
| 1183 | } |
|
| 1184 | ||
| 1185 | /** |
|
| 1186 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
|
| @@ 1194-1208 (lines=15) @@ | ||
| 1191 | * |
|
| 1192 | * @return Relation |
|
| 1193 | */ |
|
| 1194 | public function hasMany($model, $foreignKey = '', $localKey = '') |
|
| 1195 | { |
|
| 1196 | // the default local key would look like `user_id` |
|
| 1197 | // for a model named User |
|
| 1198 | if (!$foreignKey) { |
|
| 1199 | $inflector = Inflector::get(); |
|
| 1200 | $foreignKey = strtolower($inflector->underscore(static::modelName())).'_id'; |
|
| 1201 | } |
|
| 1202 | ||
| 1203 | if (!$localKey) { |
|
| 1204 | $localKey = self::DEFAULT_ID_PROPERTY; |
|
| 1205 | } |
|
| 1206 | ||
| 1207 | return new HasMany($model, $foreignKey, $localKey, $this); |
|
| 1208 | } |
|
| 1209 | ||
| 1210 | /** |
|
| 1211 | * Creates the child side of a Many-To-Many relationship. |
|
| @@ 1219-1233 (lines=15) @@ | ||
| 1216 | * |
|
| 1217 | * @return Relation |
|
| 1218 | */ |
|
| 1219 | public function belongsToMany($model, $foreignKey = '', $localKey = '') |
|
| 1220 | { |
|
| 1221 | if (!$foreignKey) { |
|
| 1222 | $foreignKey = self::DEFAULT_ID_PROPERTY; |
|
| 1223 | } |
|
| 1224 | ||
| 1225 | // the default local key would look like `user_id` |
|
| 1226 | // for a model named User |
|
| 1227 | if (!$localKey) { |
|
| 1228 | $inflector = Inflector::get(); |
|
| 1229 | $localKey = strtolower($inflector->underscore($model::modelName())).'_id'; |
|
| 1230 | } |
|
| 1231 | ||
| 1232 | return new BelongsToMany($model, $foreignKey, $localKey, $this); |
|
| 1233 | } |
|
| 1234 | ||
| 1235 | ///////////////////////////// |
|
| 1236 | // Events |
|