Skip to content

Commit fedf906

Browse files
committed
Added a way to provide both field names for belongsToMany relations
1 parent 7adbe50 commit fedf906

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/main/java/org/javawebstack/orm/Model.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,25 @@ public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other,
233233
return belongsToMany(other, pivot, Repo.get(getClass()).getInfo().getRelationField(), Repo.get(other).getInfo().getRelationField(), pivotFilter);
234234
}
235235

236-
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfFieldName, String otherFieldName) {
237-
return belongsToMany(other, pivot, selfFieldName, otherFieldName, null);
236+
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfPivotFieldName, String otherPivotFieldName) {
237+
return belongsToMany(other, pivot, selfPivotFieldName, otherPivotFieldName, null);
238238
}
239239

240-
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfFieldName, String otherFieldName, Function<Query<P>, Query<P>> pivotFilter) {
240+
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfPivotFieldName, String otherPivotFieldName, Function<Query<P>, Query<P>> pivotFilter) {
241+
return belongsToMany(other, pivot, selfPivotFieldName, otherPivotFieldName, Repo.get(getClass()).getInfo().getIdField(), Repo.get(other).getInfo().getIdField(), pivotFilter);
242+
}
243+
244+
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfPivotFieldName, String otherPivotFieldName, String selfFieldName, String otherFieldName) {
245+
return belongsToMany(other, pivot, selfPivotFieldName, otherPivotFieldName, selfFieldName, otherFieldName, null);
246+
}
247+
248+
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfPivotFieldName, String otherPivotFieldName, String selfFieldName, String otherFieldName, Function<Query<P>, Query<P>> pivotFilter) {
241249
try {
242250
Repo<?> selfRepo = Repo.get(getClass());
243251
Repo<T> otherRepo = Repo.get(other);
244-
Object id = selfRepo.getInfo().getField(selfRepo.getInfo().getIdField()).get(this);
252+
Object id = selfRepo.getInfo().getField(selfFieldName).get(this);
245253
return otherRepo.whereExists(pivot, q -> {
246-
q.where(pivot, selfFieldName, "=", id).where(pivot, otherFieldName, "=", other, otherRepo.getInfo().getIdColumn());
254+
q.where(pivot, selfPivotFieldName, "=", id).where(pivot, otherPivotFieldName, "=", other, otherFieldName);
247255
if (pivotFilter != null)
248256
q = pivotFilter.apply(q);
249257
return q;

0 commit comments

Comments
 (0)