Uploaded image for project: 'PHP ORMs'
  1. PHP ORMs
  2. PHPORM-189

laravel-mongodb - Issue #2986: whereHas does not work with ObjectId relations

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Laravel

      zliebersbach has created Issue #2986: whereHas does not work with ObjectId relations in laravel-mongodb. This Jira ticket was filed by GromNaN

      Issue Text:

      • Laravel-mongodb Version: 4.3.0
      • PHP Version: 8.2.18
      • Database Driver & Version: 1.18

      Description:

      Cannot use whereHas on relations that use ObjectId. ObjectId is the best-practice method for storing related document IDs.

      Loading the relation works as expected, but not whereHas.

      If this is possible, I propose adding the solution to the documentation, otherwise a fix should be introduced.

      Steps to reproduce

      1. `EntityB` has the following relation configured:
      ```php
      protected $casts = [
      'entity_a_id' => ObjectId::class,
      ];

      public function entityA(): BelongsTo

      { return $this->belongsTo(EntityA::class); }

      ```
      2. `EntityB::whereHas('entityA', fn ($query) => $query->where('status', Status::ADDED))->get()`

      Expected behaviour

      The following query should be executed (collected from `DB::getQueryLog()`):

      ```
      "entity_a.find(

      {"status":"A"}

      ,{"projection":

      {"_id":true}

      ,"typeMap":{"root":"array","document":"array"}})"
      "entity_b.find({"entity_a_id":{"$in":[ObjectId("66591310b42a652a7c027862"),ObjectId("66591310b42a652a7c027863")]}},{"typeMap":{"root":"array","document":"array"}})"
      ```

      Actual behaviour

      The following query is executed (collected from `DB::getQueryLog()`):

      ```
      "entity_a.find(

      {"status":"A"}

      ,{"projection":

      {"_id":true}

      ,"typeMap":{"root":"array","document":"array"}})"
      "entity_b.find({"entity_a_id":{"$in":["66591310b42a652a7c027862","66591310b42a652a7c027863"]}},{"typeMap":{"root":"array","document":"array"}})"
      ```

      <details><summary><b>Logs</b>:</summary>
      </details>

            Assignee:
            Unassigned Unassigned
            Reporter:
            dbeng-pm-bot PM Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: