Using DiscriminatorMap - a child node uses the parents object for an ID field #6322

Closed
opened 2026-01-22 15:30:53 +01:00 by admin · 7 comments
Owner

Originally created by @rlweb on GitHub (Oct 1, 2019).

Originally assigned to: @lcobucci on GitHub.

Bug Report

Q A
BC Break ?
Version 2.6.3

Summary

image_2019-09-30_19-28-32

When using the DiscriminatorMap on a child node of an object, it gives the parent object to the ID property of the ManyToOne object.

As shown above is a Note object which contains a Person ManyToOne entity

Current behavior

The ID of the child object (via the discriminator map) is the parent object.

How to reproduce

See https://github.com/rlweb/orm/commit/54fc7414a2593eafdfb1abe21662444e72755b10 for an example of my issue which gives the following:
image

Exception : [TypeError] Return value of Doctrine\Tests\Functional\Ticket\Person::getId() must be of the type int, object returned

With queries:
14. SQL: 'SELECT t0.id AS id_2, t0.first_name AS first_name_3, t0.type, t1.job_title AS job_title_4 FROM Person t0 LEFT JOIN Staff t1 ON t0.id = t1.id WHERE t0.id = ?' Params: 1
13. SQL: 'SELECT t0.id AS id_1, t0.last_updated_by AS last_updated_by_2 FROM Note t0 WHERE t0.id = ?' Params: 1
12. SQL: '"COMMIT"' Params: 
11. SQL: 'INSERT INTO Note (last_updated_by) VALUES (?)' Params: 1
10. SQL: '"START TRANSACTION"' Params: 
9. SQL: 'SELECT t1.id AS id_2, t1.first_name AS first_name_3, t0.job_title AS job_title_4, t1.type FROM Staff t0 INNER JOIN Person t1 ON t0.id = t1.id WHERE t1.id = ?' Params: 1
8. SQL: '"COMMIT"' Params: 
7. SQL: 'INSERT INTO Staff (id, job_title) VALUES (?, ?)' Params: 1, 'dev'
6. SQL: 'INSERT INTO Person (first_name, type) VALUES (?, ?)' Params: 'Rhys', 'staff'
5. SQL: '"START TRANSACTION"' Params: 
4. SQL: 'CREATE TABLE Staff (id INTEGER UNSIGNED NOT NULL, job_title VARCHAR(124) NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_83AFDC96BF396750 FOREIGN KEY (id) REFERENCES Person (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)' Params: 
3. SQL: 'CREATE TABLE Person (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, first_name VARCHAR(50) NOT NULL, type VARCHAR(255) NOT NULL)' Params: 
2. SQL: 'CREATE INDEX IDX_6F8F552AFF8A180B ON Note (last_updated_by)' Params: 
1. SQL: 'CREATE TABLE Note (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, last_updated_by INTEGER UNSIGNED DEFAULT NULL, CONSTRAINT FK_6F8F552AFF8A180B FOREIGN KEY (last_updated_by) REFERENCES Person (id) ON DELETE RESTRICT NOT DEFERRABLE INITIALLY IMMEDIATE)' Params: 

Trace:
/repo/tmp/orm/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7847Test.php:53
/repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestCase.php:1071
/repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestCase.php:939
/repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestResult.php:698
/repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestCase.php:894
/repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestSuite.php:755
/repo/tmp/orm/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:545
/repo/tmp/orm/vendor/phpunit/phpunit/src/TextUI/Command.php:195
/repo/tmp/orm/vendor/phpunit/phpunit/src/TextUI/Command.php:148
/repo/tmp/orm/vendor/phpunit/phpunit/phpunit:53

 /repo/tmp/orm/tests/Doctrine/Tests/OrmFunctionalTestCase.php:819
 
 Caused by
 TypeError: Return value of Doctrine\Tests\Functional\Ticket\Person::getId() must be of the type int, object returned
 
 /repo/tmp/orm/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7847Test.php:134
 /repo/tmp/orm/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7847Test.php:53

Expected behavior

The ID to be correctly set as an integer.

Originally created by @rlweb on GitHub (Oct 1, 2019). Originally assigned to: @lcobucci on GitHub. ### Bug Report | Q | A |------------ | ------ | BC Break | ? | Version | 2.6.3 #### Summary ![image_2019-09-30_19-28-32](https://user-images.githubusercontent.com/173838/65967146-09d9c600-e459-11e9-9f9c-63ab89527fa6.png) When using the DiscriminatorMap on a child node of an object, it gives the parent object to the ID property of the ManyToOne object. As shown above is a Note object which contains a Person ManyToOne entity #### Current behavior The ID of the child object (via the discriminator map) is the parent object. #### How to reproduce See https://github.com/rlweb/orm/commit/54fc7414a2593eafdfb1abe21662444e72755b10 for an example of my issue which gives the following: ![image](https://user-images.githubusercontent.com/173838/65968006-7a351700-e45a-11e9-90fe-1d4cb55562b0.png) ``` Exception : [TypeError] Return value of Doctrine\Tests\Functional\Ticket\Person::getId() must be of the type int, object returned With queries: 14. SQL: 'SELECT t0.id AS id_2, t0.first_name AS first_name_3, t0.type, t1.job_title AS job_title_4 FROM Person t0 LEFT JOIN Staff t1 ON t0.id = t1.id WHERE t0.id = ?' Params: 1 13. SQL: 'SELECT t0.id AS id_1, t0.last_updated_by AS last_updated_by_2 FROM Note t0 WHERE t0.id = ?' Params: 1 12. SQL: '"COMMIT"' Params: 11. SQL: 'INSERT INTO Note (last_updated_by) VALUES (?)' Params: 1 10. SQL: '"START TRANSACTION"' Params: 9. SQL: 'SELECT t1.id AS id_2, t1.first_name AS first_name_3, t0.job_title AS job_title_4, t1.type FROM Staff t0 INNER JOIN Person t1 ON t0.id = t1.id WHERE t1.id = ?' Params: 1 8. SQL: '"COMMIT"' Params: 7. SQL: 'INSERT INTO Staff (id, job_title) VALUES (?, ?)' Params: 1, 'dev' 6. SQL: 'INSERT INTO Person (first_name, type) VALUES (?, ?)' Params: 'Rhys', 'staff' 5. SQL: '"START TRANSACTION"' Params: 4. SQL: 'CREATE TABLE Staff (id INTEGER UNSIGNED NOT NULL, job_title VARCHAR(124) NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_83AFDC96BF396750 FOREIGN KEY (id) REFERENCES Person (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)' Params: 3. SQL: 'CREATE TABLE Person (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, first_name VARCHAR(50) NOT NULL, type VARCHAR(255) NOT NULL)' Params: 2. SQL: 'CREATE INDEX IDX_6F8F552AFF8A180B ON Note (last_updated_by)' Params: 1. SQL: 'CREATE TABLE Note (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, last_updated_by INTEGER UNSIGNED DEFAULT NULL, CONSTRAINT FK_6F8F552AFF8A180B FOREIGN KEY (last_updated_by) REFERENCES Person (id) ON DELETE RESTRICT NOT DEFERRABLE INITIALLY IMMEDIATE)' Params: Trace: /repo/tmp/orm/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7847Test.php:53 /repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestCase.php:1071 /repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestCase.php:939 /repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestResult.php:698 /repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestCase.php:894 /repo/tmp/orm/vendor/phpunit/phpunit/src/Framework/TestSuite.php:755 /repo/tmp/orm/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:545 /repo/tmp/orm/vendor/phpunit/phpunit/src/TextUI/Command.php:195 /repo/tmp/orm/vendor/phpunit/phpunit/src/TextUI/Command.php:148 /repo/tmp/orm/vendor/phpunit/phpunit/phpunit:53 /repo/tmp/orm/tests/Doctrine/Tests/OrmFunctionalTestCase.php:819 Caused by TypeError: Return value of Doctrine\Tests\Functional\Ticket\Person::getId() must be of the type int, object returned /repo/tmp/orm/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7847Test.php:134 /repo/tmp/orm/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7847Test.php:53 ``` #### Expected behavior The ID to be correctly set as an integer.
admin added the BugInvalid labels 2026-01-22 15:30:53 +01:00
admin closed this issue 2026-01-22 15:30:53 +01:00
Author
Owner

@Ocramius commented on GitHub (Oct 1, 2019):

I'm not sure what's being asked: it is not possible to override property types in a table inheritance, is that what you are trying to do?

@Ocramius commented on GitHub (Oct 1, 2019): I'm not sure what's being asked: it is not possible to override property types in a table inheritance, is that what you are trying to do?
Author
Owner

@rlweb commented on GitHub (Oct 1, 2019):

Hi @Ocramius, I'm not trying to override property types, rather I would like to use class table inheritance to return the type of Person who updated the note.

So like below we have 2 types of users: contact and staff. We would like to link notes to the Person entity.

image

It seems odd that doctrine cannot pass back the correct ID of the person entity but can link the entities correctly which is the bug shown in the unit test :)

@rlweb commented on GitHub (Oct 1, 2019): Hi @Ocramius, I'm not trying to override property types, rather I would like to use class table inheritance to return the type of Person who updated the note. So like below we have 2 types of users: contact and staff. We would like to link notes to the Person entity. ![image](https://user-images.githubusercontent.com/173838/65969884-722aa680-e45d-11e9-97bc-e5afe9bf3801.png) It seems odd that doctrine cannot pass back the correct ID of the person entity but can link the entities correctly which is the bug shown in the unit test :)
Author
Owner

@Ocramius commented on GitHub (Oct 1, 2019):

Ah, I totally missed the test.

I think this is a bug caused by silly proxy optimisations.

Do you have a chance to check if this also applies to master?

@Ocramius commented on GitHub (Oct 1, 2019): Ah, I totally missed the test. I think this is a bug caused by silly proxy optimisations. Do you have a chance to check if this also applies to `master`?
Author
Owner

@rlweb commented on GitHub (Oct 1, 2019):

It works in master! Thanks @Ocramius :) Any idea on the progress of v3 and would it be released in the next year?

@rlweb commented on GitHub (Oct 1, 2019): It works in master! Thanks @Ocramius :) Any idea on the progress of v3 and would it be released in the next year?
Author
Owner

@lcobucci commented on GitHub (Oct 2, 2019):

@rlweb there's an error on your mapping, which is making the entities to be hydrated in the wrong way. You're telling the ORM that lastUpdatedBy is inversedBy Person.id, which isn't correct since Person is clueless about Note.

Here's the fix:

/**
 * @Entity
 */
class Note
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     * @var int
     */
    protected $id;
    /**
-     * @ManyToOne(targetEntity="Person", inversedBy="id")
+     * @ManyToOne(targetEntity="Person")
     * @JoinColumn(name="last_updated_by", referencedColumnName="id", onDelete="RESTRICT")
     * @var Person
     */
    private $lastUpdatedBy;
@lcobucci commented on GitHub (Oct 2, 2019): @rlweb there's an error on your mapping, which is making the entities to be hydrated in the wrong way. You're telling the ORM that `lastUpdatedBy` is inversedBy `Person.id`, which isn't correct since `Person` is clueless about `Note`. Here's the fix: ```diff /** * @Entity */ class Note { /** * @Id * @Column(type="integer") * @GeneratedValue * @var int */ protected $id; /** - * @ManyToOne(targetEntity="Person", inversedBy="id") + * @ManyToOne(targetEntity="Person") * @JoinColumn(name="last_updated_by", referencedColumnName="id", onDelete="RESTRICT") * @var Person */ private $lastUpdatedBy; ```
Author
Owner

@lcobucci commented on GitHub (Oct 2, 2019):

Closing as invalid as per my comment 👍

@lcobucci commented on GitHub (Oct 2, 2019): Closing as `invalid` as per my comment :+1:
Author
Owner

@lcobucci commented on GitHub (Oct 2, 2019):

Any idea on the progress of v3 and would it be released in the next year?

@rlweb no idea on the release date, yet

@lcobucci commented on GitHub (Oct 2, 2019): > Any idea on the progress of v3 and would it be released in the next year? @rlweb no idea on the release date, yet
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6322