ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Blocker - P1
    • None
    • Affects Version/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Silent wrong results found by comparing the embedded relation queries with the same relations on SQL Eloquent (SQLite hasMany/hasOne). Both are in code paths that the whereHas() change (PHPLARA-328) does not touch, but they return data without any error.

      1. Aggregates lose the embedded field when the query selects columns

      Both functions return 0 and false when the query has a select(), and loadCount() and loadExists() always do. For example, User::select('name')>withCount('addresses')>get() gives addresses_count = 0 for every user, and User::orderBy('name')>get()>loadCount('addresses') gives the same zero.

      Cause: QueriesRelationshipAggregates::getAggregateParentKey() returns null for EmbedsOneOrMany, so withAggregate() does not add the key to the projection. The embedded aggregate is then computed from $model->addresses()->getResults(), and the field is absent from the selected document. Collection::loadAggregate() selects only the primary key, so loadCount() and loadExists() are always wrong.

      Fix direction: when the relation is an EmbedsOneOrMany and the query has columns, add the relation local key to the select, as withAggregate() already does with the parent key of other relations.

      2. Constraints on eager loaded embedded relations are ignored

      • User::with(['addresses' => fn ($query) => $query->where('city', 'Paris')])->get() loads every embedded address, while SQL loads only Paris.
      • User::withWhereHas('addresses', fn ($query) => $query->where('city', 'Paris'))->get() narrows the parent documents through whereHas() but the loaded relation stays unfiltered.

      Cause: EmbedsOneOrMany::addEagerConstraints() is empty and match() rebuilds the relation from the parent with $model->$relation()->getResults(), so the constrained query is discarded. QueriesRelationshipAggregates already rejects constraints on embedded relations with a LogicException, which is the loud behaviour to mirror at minimum.

      Test cases to add (Alice has 2 addresses, Bob has 1, Carol has 0)

      • User::select('name')>withCount('addresses')>orderBy('name')->get() returns 2, 1, 0.
      • User::select('name')>withExists('addresses')>orderBy('name')->get() returns true, true, false.
      • User::orderBy('name')>get()>loadCount('addresses') sets addresses_count to 2, 1, 0.
      • User::orderBy('name')>get()>loadExists('addresses') sets addresses_exists to true, true, false.
      • loadCount('father') and loadExists('father') on the embedsOne relation return the same values as the SQL hasOne baseline.
      • User::with(['addresses' => fn ($query) => $query->where('city', 'Paris')])->get() loads only Paris for Alice, and nothing for Bob and Carol.
      • User::withWhereHas('addresses', fn ($query) => $query->where('city', 'Paris'))->get() returns Alice with only Paris loaded.
      • If filtering cannot be supported on an embedded eager load, the call throws a clear LogicException instead of returning unfiltered documents.

      Run each case against a SQLite hasMany/hasOne baseline as well, to keep the SQL parity check.

              Assignee:
              Unassigned
              Reporter:
              Jérôme Tamarelle
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated: