| @@ 1037-1051 (lines=15) @@ | ||
| 1034 | * |
|
| 1035 | * @return Relation |
|
| 1036 | */ |
|
| 1037 | public function hasOne($model, $foreignKey = '', $localKey = '') |
|
| 1038 | { |
|
| 1039 | // the default local key would look like `user_id` |
|
| 1040 | // for a model named User |
|
| 1041 | if (!$foreignKey) { |
|
| 1042 | $inflector = Inflector::get(); |
|
| 1043 | $foreignKey = strtolower($inflector->underscore(static::modelName())).'_id'; |
|
| 1044 | } |
|
| 1045 | ||
| 1046 | if (!$localKey) { |
|
| 1047 | $localKey = self::DEFAULT_ID_PROPERTY; |
|
| 1048 | } |
|
| 1049 | ||
| 1050 | return new HasOne($model, $foreignKey, $localKey, $this); |
|
| 1051 | } |
|
| 1052 | ||
| 1053 | /** |
|
| 1054 | * Creates the child side of a One-To-One or One-To-Many relationship. |
|
| @@ 1062-1076 (lines=15) @@ | ||
| 1059 | * |
|
| 1060 | * @return Relation |
|
| 1061 | */ |
|
| 1062 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
|
| 1063 | { |
|
| 1064 | if (!$foreignKey) { |
|
| 1065 | $foreignKey = self::DEFAULT_ID_PROPERTY; |
|
| 1066 | } |
|
| 1067 | ||
| 1068 | // the default local key would look like `user_id` |
|
| 1069 | // for a model named User |
|
| 1070 | if (!$localKey) { |
|
| 1071 | $inflector = Inflector::get(); |
|
| 1072 | $localKey = strtolower($inflector->underscore($model::modelName())).'_id'; |
|
| 1073 | } |
|
| 1074 | ||
| 1075 | return new BelongsTo($model, $foreignKey, $localKey, $this); |
|
| 1076 | } |
|
| 1077 | ||
| 1078 | /** |
|
| 1079 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
|
| @@ 1087-1101 (lines=15) @@ | ||
| 1084 | * |
|
| 1085 | * @return Relation |
|
| 1086 | */ |
|
| 1087 | public function hasMany($model, $foreignKey = '', $localKey = '') |
|
| 1088 | { |
|
| 1089 | // the default local key would look like `user_id` |
|
| 1090 | // for a model named User |
|
| 1091 | if (!$foreignKey) { |
|
| 1092 | $inflector = Inflector::get(); |
|
| 1093 | $foreignKey = strtolower($inflector->underscore(static::modelName())).'_id'; |
|
| 1094 | } |
|
| 1095 | ||
| 1096 | if (!$localKey) { |
|
| 1097 | $localKey = self::DEFAULT_ID_PROPERTY; |
|
| 1098 | } |
|
| 1099 | ||
| 1100 | return new HasMany($model, $foreignKey, $localKey, $this); |
|
| 1101 | } |
|
| 1102 | ||
| 1103 | /** |
|
| 1104 | * Creates the child side of a Many-To-Many relationship. |
|
| @@ 1112-1126 (lines=15) @@ | ||
| 1109 | * |
|
| 1110 | * @return Relation |
|
| 1111 | */ |
|
| 1112 | public function belongsToMany($model, $foreignKey = '', $localKey = '') |
|
| 1113 | { |
|
| 1114 | if (!$foreignKey) { |
|
| 1115 | $foreignKey = self::DEFAULT_ID_PROPERTY; |
|
| 1116 | } |
|
| 1117 | ||
| 1118 | // the default local key would look like `user_id` |
|
| 1119 | // for a model named User |
|
| 1120 | if (!$localKey) { |
|
| 1121 | $inflector = Inflector::get(); |
|
| 1122 | $localKey = strtolower($inflector->underscore($model::modelName())).'_id'; |
|
| 1123 | } |
|
| 1124 | ||
| 1125 | return new BelongsToMany($model, $foreignKey, $localKey, $this); |
|
| 1126 | } |
|
| 1127 | ||
| 1128 | ///////////////////////////// |
|
| 1129 | // Events |
|