Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pervozdanniy opened this issue Dec 25, 2020 · 1 comment
Closed

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

pervozdanniy opened this issue Dec 25, 2020 · 1 comment

Comments

@pervozdanniy
Copy link

pervozdanniy commented Dec 25, 2020

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

@divine
Copy link
Contributor

divine commented Dec 26, 2020

Duplicate of #2040, see also #1974 (comment)

@divine divine closed this as completed Dec 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants