Skip to content

[Feature Request] Configuration for relation keys types needed #2167

Closed
@pervozdanniy

Description

@pervozdanniy

Is your feature request related to a problem?

I have an 1-to-1 relation for 2 tables with foreign key saved as ObjectId:
Sales:

{
 _id: ObjectId("some_id")
 idUser: ObjectId("foreignKey")
  ...otherFields
}

Users:

{
   _id: ObjectId("foreignKey"),
   ...otherFileds
}

Relation is defined on Sales model as follows:

public function user(): BelongsTo
{
     return $this->belongsTo(User::class, 'idUser', '_id', 'user');
}

When I'm trying to apply whereHas condition:

$query->whereHas('user', static fn (Builder $query): Builder => $query->where('username', $keyword));

it returns no results for relation fetching query (from laravel debugger):

users.find({"username":"enegy5"},{"projection":{"_id":true},"typeMap":{"root":"array","document":"array"}})
sales.find({"idUser":{"$in":["5f5d18344504d801ea0a1b23"]}},{"sort":{"dateCreate":-1},"limit":10,"typeMap":{"root":"array","document":"array"}})

while Sale document with such idUser does exist.

Describe the solution you'd like

I'd like to have an configuration array for each model extended from Jenssegers\Mongodb\Eloquent\Model to have an ability to somehow say if I need an ObjectId or a string format of foreign key, e.g.

protected $relatedKeyTypes = [
   'idUser' => 'objectId',
   'showroomId' => 'string'
];

which should be applied to Jenssegers\Mongodb\Helpers\QueriesRelationships::getConstrainedRelatedIds() method

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions