ManyToOne association entity identifier #5424

Closed
opened 2026-01-22 15:07:22 +01:00 by admin · 4 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:22 +01:00
admin closed this issue 2026-01-22 15:07:22 +01:00
Author
Owner

@lcobucci commented on GitHub (Feb 23, 2017):

@podorozhny it seems odd, can you please send a PR with a test that reproduce that issue (we can use e4704beaf9/tests/Doctrine/Tests/ORM/Functional/Ticket/GH5762Test.php as example).

@lcobucci commented on GitHub (Feb 23, 2017): @podorozhny it seems odd, can you please send a PR with a test that reproduce that issue (we can use https://github.com/doctrine/doctrine2/blob/e4704beaf9fad5a521fe7592dec382ae209b3cc1/tests/Doctrine/Tests/ORM/Functional/Ticket/GH5762Test.php as example).
Author
Owner

@podorozhny commented on GitHub (Feb 23, 2017):

@lcobucci

https://github.com/doctrine/doctrine2/pull/6301

➜  doctrine2 git:(master) ✗ ./vendor/bin/phpunit --filter=GH6297Test
PHPUnit 5.7.14-1-g7d5e096 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 555 ms, Memory: 36.00MB

There was 1 error:

1) Doctrine\Tests\ORM\Functional\Ticket\GH6297Test::testIssue
Exception: [PHPUnit_Framework_Error_Notice] Undefined index: secondaryId

With queries:
6. SQL: '"ROLLBACK"' Params:
5. SQL: 'INSERT INTO groups (primary_id, secondary_id) VALUES (?, ?)' Params: 1, 'test'
4. SQL: '"START TRANSACTION"' Params:
3. SQL: 'CREATE TABLE groups (primary_id INTEGER NOT NULL, secondary_id VARCHAR(255) NOT NULL, PRIMARY KEY(primary_id))' Params:
2. SQL: 'CREATE INDEX IDX_1483A5E9FE54D947 ON users (group_id)' Params:
1. SQL: 'CREATE TABLE users (id INTEGER NOT NULL, group_id VARCHAR(255) NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_1483A5E9FE54D947 FOREIGN KEY (group_id) REFERENCES groups (secondary_id) NOT DEFERRABLE INITIALLY IMMEDIATE)' Params:

Trace:
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:672
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:694
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:271
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/UnitOfWork.php:1003
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/UnitOfWork.php:371
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/EntityManager.php:359
/Users/fifteen/dev/doctrine2/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6297Test.php:46
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestCase.php:1120
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestCase.php:971
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestResult.php:709
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestCase.php:926
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:521
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/TextUI/Command.php:188
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/TextUI/Command.php:118
/Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/phpunit:52


/Users/fifteen/dev/doctrine2/tests/Doctrine/Tests/OrmFunctionalTestCase.php:789

Caused by
PHPUnit_Framework_Error_Notice: Undefined index: secondaryId

/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:672
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:694
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:271
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/UnitOfWork.php:1003
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/UnitOfWork.php:371
/Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/EntityManager.php:359
/Users/fifteen/dev/doctrine2/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6297Test.php:46

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
@podorozhny commented on GitHub (Feb 23, 2017): @lcobucci https://github.com/doctrine/doctrine2/pull/6301 ``` ➜ doctrine2 git:(master) ✗ ./vendor/bin/phpunit --filter=GH6297Test PHPUnit 5.7.14-1-g7d5e096 by Sebastian Bergmann and contributors. E 1 / 1 (100%) Time: 555 ms, Memory: 36.00MB There was 1 error: 1) Doctrine\Tests\ORM\Functional\Ticket\GH6297Test::testIssue Exception: [PHPUnit_Framework_Error_Notice] Undefined index: secondaryId With queries: 6. SQL: '"ROLLBACK"' Params: 5. SQL: 'INSERT INTO groups (primary_id, secondary_id) VALUES (?, ?)' Params: 1, 'test' 4. SQL: '"START TRANSACTION"' Params: 3. SQL: 'CREATE TABLE groups (primary_id INTEGER NOT NULL, secondary_id VARCHAR(255) NOT NULL, PRIMARY KEY(primary_id))' Params: 2. SQL: 'CREATE INDEX IDX_1483A5E9FE54D947 ON users (group_id)' Params: 1. SQL: 'CREATE TABLE users (id INTEGER NOT NULL, group_id VARCHAR(255) NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_1483A5E9FE54D947 FOREIGN KEY (group_id) REFERENCES groups (secondary_id) NOT DEFERRABLE INITIALLY IMMEDIATE)' Params: Trace: /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:672 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:694 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:271 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/UnitOfWork.php:1003 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/UnitOfWork.php:371 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/EntityManager.php:359 /Users/fifteen/dev/doctrine2/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6297Test.php:46 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestCase.php:1120 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestCase.php:971 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestResult.php:709 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestCase.php:926 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:521 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/TextUI/Command.php:188 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/src/TextUI/Command.php:118 /Users/fifteen/dev/doctrine2/vendor/phpunit/phpunit/phpunit:52 /Users/fifteen/dev/doctrine2/tests/Doctrine/Tests/OrmFunctionalTestCase.php:789 Caused by PHPUnit_Framework_Error_Notice: Undefined index: secondaryId /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:672 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:694 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:271 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/UnitOfWork.php:1003 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/UnitOfWork.php:371 /Users/fifteen/dev/doctrine2/lib/Doctrine/ORM/EntityManager.php:359 /Users/fifteen/dev/doctrine2/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6297Test.php:46 ERRORS! Tests: 1, Assertions: 0, Errors: 1. ```
Author
Owner

@lcobucci commented on GitHub (Feb 23, 2017):

@podorozhny awesome, thanks!

@lcobucci commented on GitHub (Feb 23, 2017): @podorozhny awesome, thanks!
Author
Owner

@Ocramius commented on GitHub (Feb 25, 2017):

Closing as invalid as per #6301 findings

@Ocramius commented on GitHub (Feb 25, 2017): Closing as `invalid` as per #6301 findings
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5424