Cannot index-by related entity #6210

Closed
opened 2026-01-22 15:28:58 +01:00 by admin · 3 comments
Owner

Originally created by @crtl on GitHub (Mar 29, 2019).

Bug Report

Q A
BC Break no
Version 2.6.3

Summary

When trying to index by a property that is a mapped relation the following error is thrown:

Cannot match on App\Entity\Entity::thirdEntity with a non-object value. 
Matching objects by id is not compatible with matching on an in-memory collection, which compares objects by reference.

Current behavior

Exception is thrown

How to reproduce

Create the following mapping:


<entity name="App\Entity\User">
    <id name="id" />
</entity>

<entity name="App\Entity\Event">
    <id name="id" />

    <one-to-many field="participants" target-entity="App\Entity\Event\Participant" mapped-by="event" index-by="user" fetch="EXTRA_LAZY" >
        <cascade>
            <cascade-all/>
        </cascade>
    </one-to-many>
</entity>

<entity name="App\Entity\Event\Participant">
    <id name="id" />

    <many-to-one field="user" target-entity="App\Entity\User" />
    <many-to-one field="event" target-entity="App\Entity\Event" />

</entity>

class Event {

    protected $participants;

    public function __construct() {
        $this->participants = new ArrayCollection();
    }

    public function isParticipating($user) {
        $id = $user instanceof User ? $user->getId() : $user;

        return $this->participants->containsKey($id); //Throws exception, passing $user (entity) throws illegal offset
    }
}

Expected behavior

The participants should be correctly mapped by their related users id or the schema validator should throw an error when validating the schema (schema is valid).

Originally created by @crtl on GitHub (Mar 29, 2019). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 2.6.3 #### Summary When trying to index by a property that is a mapped relation the following error is thrown: ``` Cannot match on App\Entity\Entity::thirdEntity with a non-object value. Matching objects by id is not compatible with matching on an in-memory collection, which compares objects by reference. ``` #### Current behavior Exception is thrown #### How to reproduce Create the following mapping: ```xml <entity name="App\Entity\User"> <id name="id" /> </entity> <entity name="App\Entity\Event"> <id name="id" /> <one-to-many field="participants" target-entity="App\Entity\Event\Participant" mapped-by="event" index-by="user" fetch="EXTRA_LAZY" > <cascade> <cascade-all/> </cascade> </one-to-many> </entity> <entity name="App\Entity\Event\Participant"> <id name="id" /> <many-to-one field="user" target-entity="App\Entity\User" /> <many-to-one field="event" target-entity="App\Entity\Event" /> </entity> ``` ```php class Event { protected $participants; public function __construct() { $this->participants = new ArrayCollection(); } public function isParticipating($user) { $id = $user instanceof User ? $user->getId() : $user; return $this->participants->containsKey($id); //Throws exception, passing $user (entity) throws illegal offset } } ``` #### Expected behavior The participants should be correctly mapped by their related users id or the schema validator should throw an error when validating the schema (schema is valid).
admin closed this issue 2026-01-22 15:28:58 +01:00
Author
Owner

@andrews05 commented on GitHub (Nov 20, 2019):

Have you tried using the name of the join column, e.g. "user_id" instead of just "user"? This is working for me in v3.

@andrews05 commented on GitHub (Nov 20, 2019): Have you tried using the name of the join column, e.g. "user_id" instead of just "user"? This is working for me in v3.
Author
Owner

@andrews05 commented on GitHub (Nov 20, 2019):

What I can't do is use INDEX BY in a query in the same manner.
SELECT p FROM Participant INDEX BY p.user_id: "Class Participant has no field or association named related_id"
SELECT p FROM Participant INDEX BY p.user: "Invalid PathExpression. Must be a StateFieldPathExpression."

@andrews05 commented on GitHub (Nov 20, 2019): What I can't do is use INDEX BY in a query in the same manner. `SELECT p FROM Participant INDEX BY p.user_id`: "Class Participant has no field or association named related_id" `SELECT p FROM Participant INDEX BY p.user`: "Invalid PathExpression. Must be a StateFieldPathExpression."
Author
Owner

@beberlei commented on GitHub (Feb 6, 2021):

Fixed and targeted for 2.9.0

@beberlei commented on GitHub (Feb 6, 2021): Fixed and targeted for 2.9.0
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6210