laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

all ObjectId fields (including polymorphic relations must be `_id` or ended with `._id`)

Open farmani opened this issue 3 years ago • 3 comments

  • Laravel-mongodb Version: 3.8.4
  • PHP Version: 8.1.1
  • Database Driver & Version: 4.2

Description:

Bases on this code https://github.com/jenssegers/laravel-mongodb/blob/master/src/Query/Builder.php#L939 only _id and all fields ended with ._id will cast into the ObjectId before running where queries this is not mentioned anywhere in the doc. this specifically make problems when you are going to use polymorphic relations in the models because relation would be name_id and name_type and so they won't cast into the ObjectId

naming polymophic relation to something ended with . is not a big deal but at least should mention in the doc or consider to find a better solution like looking for a defined property in the model class like ObjectIds or even better let's developer use $casts and rely on it

farmani avatar Mar 25 '22 11:03 farmani

ObjectId cast have been added to the upcoming version 4.0. Does it solves this issue?

GromNaN avatar Aug 24 '23 09:08 GromNaN

ObjectId cast have been added to the upcoming version 4.0. Does it solves this issue?

ObjectId cast doesn't solve this issue.

Model's id or fields with ObjectId cast always return its value as string. https://github.com/mongodb/laravel-mongodb/blob/master/src/Eloquent/Casts/ObjectId.php#L26 https://github.com/mongodb/laravel-mongodb/blob/master/src/Eloquent/Model.php#L68

Builder only casts into ObjectId columns with the name _id or ending with ._id.

p4vc0d3 avatar Sep 05 '23 19:09 p4vc0d3

this also will make problem in relations the only possible solution is to overwrite getIdAttribute() in the model.

public function getIdAttribute($value = null)
{
    return $value;
}

farmani avatar Oct 26 '23 06:10 farmani