ManyToOne association entity identifier #5422

Open
opened 2026-01-22 15:07:20 +01:00 by admin · 0 comments
Owner

Originally created by @podorozhny on GitHub (Feb 22, 2017).

Originally assigned to: @Ocramius on GitHub.

Hi everyone.

I ran into a problem using many-to-one association between entities while using custom property (not @ORM\Id() one) to join mapped entity.

Simplified example:

class User {
    /**
     * @ORM\ManyToOne(targetEntity="...\Group", inversedBy="users")
     * @ORM\JoinColumn(referencedColumnName="second_id", nullable=false)
     */
    private $group;

    ...
}

class Group {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $secondId;

    /**
     * @ORM\OneToMany(targetEntity="...\User", mappedBy="users")
     */
    private $users;
}

bin/console doctrine:schema:validate said all mappings are okay, but when I tried to insert new user I got this error:

CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Notice: Undefined index: secondId" at /application/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php line 671

https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L671

I got ['id' => 100500] here instead of ['secondId' => 100500].

Thanks.

Originally created by @podorozhny on GitHub (Feb 22, 2017). Originally assigned to: @Ocramius on GitHub. Hi everyone. I ran into a problem using many-to-one association between entities while using custom property (not @ORM\Id() one) to join mapped entity. Simplified example: ``` php class User { /** * @ORM\ManyToOne(targetEntity="...\Group", inversedBy="users") * @ORM\JoinColumn(referencedColumnName="second_id", nullable=false) */ private $group; ... } class Group { /** * @ORM\Id * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string") */ private $secondId; /** * @ORM\OneToMany(targetEntity="...\User", mappedBy="users") */ private $users; } ``` `bin/console doctrine:schema:validate` said all mappings are okay, but when I tried to insert new user I got this error: ``` CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Notice: Undefined index: secondId" at /application/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php line 671 ``` https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L671 I got `['id' => 100500]` [here](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L658) instead of `['secondId' => 100500]`. Thanks.
admin added the BugInvalid labels 2026-01-22 15:07:20 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5422