DDC-1397: Accesing a OneToMany relation with Child Classes and OneToOne relations. #1749

Closed
opened 2026-01-22 13:24:29 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Sep 29, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user gbrunacci:

I am facing a small issue on Doctrine 2 that causes OneToOne relation needed to be part of the parent class. The error occurs when doing $content->getVersions() but not when doing $em->find("Model\Lantern\Content\Version", $id). The error message is:

Warning: Undefined index: summary in ORM/UnitOfWork.php:2012
Fatal error: Call to a member function setValue() on a non-object in ORM/UnitOfWork.php on line 2013

Error replication:

Simply by having a look at the model we can see what's causing the Bug, will avoid class annotations to make it simpler:

class Content{
    /****
    * @OneToMany(targetEntity="Version", mappedBy="content")
    */
    private $versions;
...
}

class Version{
    /****
     * @ManyToOne(targetEntity="Content", inversedBy="versions")
     * @JoinColumn(name="content*id", referencedColumnName="content*id")
     */
    private $content;
...
}

class Article extends Version{
    /*** @OneToOne(targetEntity="Model\Lantern\Content\Fieldset\Summary", mappedBy="version", cascade={"persist", "delete"}) **/
    private $summary;  
...
}

class Summary{
    /****
     * @OneToOne(targetEntity="Model\Lantern\Content\Version", inversedBy="summary")
     * @JoinColumn(name="version*id", referencedColumnName="version*id")
     */
    private $version;    
...
}

As you can see, Article::$summary targets Summary::$version, but Summary::$version targets Version::$summary. This in OOP is valid as Version is contained inside Article, so doing a downcasting will get Article::summary.

The reason I believe why using $em->find works is that $em->find already knows which child class is before doing map, and relations works fine.
On the other hand, when doing $content->getVersions(), Content::$versions targets Version, and looks like Doctrine does not resolve which child class it before mapping OneToOne relationshipg.

This bug doesn't occur with OneToMany or ManyToMany relationships.

Originally created by @doctrinebot on GitHub (Sep 29, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user gbrunacci: I am facing a small issue on Doctrine 2 that causes OneToOne relation needed to be part of the parent class. The error occurs when doing $content->getVersions() but not when doing $em->find("Model\Lantern\Content\Version", $id). The error message is: ``` Warning: Undefined index: summary in ORM/UnitOfWork.php:2012 Fatal error: Call to a member function setValue() on a non-object in ORM/UnitOfWork.php on line 2013 ``` Error replication: Simply by having a look at the model we can see what's causing the Bug, will avoid class annotations to make it simpler: ``` java class Content{ /**** * @OneToMany(targetEntity="Version", mappedBy="content") */ private $versions; ... } class Version{ /**** * @ManyToOne(targetEntity="Content", inversedBy="versions") * @JoinColumn(name="content*id", referencedColumnName="content*id") */ private $content; ... } class Article extends Version{ /*** @OneToOne(targetEntity="Model\Lantern\Content\Fieldset\Summary", mappedBy="version", cascade={"persist", "delete"}) **/ private $summary; ... } class Summary{ /**** * @OneToOne(targetEntity="Model\Lantern\Content\Version", inversedBy="summary") * @JoinColumn(name="version*id", referencedColumnName="version*id") */ private $version; ... } ``` As you can see, Article::$summary targets Summary::$version, but Summary::$version targets Version::$summary. This in OOP is valid as Version is contained inside Article, so doing a downcasting will get Article::summary. The reason I believe why using $em->find works is that $em->find already knows which child class is before doing map, and relations works fine. On the other hand, when doing $content->getVersions(), Content::$versions targets Version, and looks like Doctrine does not resolve which child class it before mapping OneToOne relationshipg. This bug doesn't occur with OneToMany or ManyToMany relationships.
admin added the Bug label 2026-01-22 13:24:29 +01:00
admin closed this issue 2026-01-22 13:24:30 +01:00
Author
Owner

@doctrinebot commented on GitHub (Sep 29, 2011):

Comment created by gbrunacci:

A side but useful note: when I move @OneToOne relation to the parent class Version, The error doesn't happen.

@doctrinebot commented on GitHub (Sep 29, 2011): Comment created by gbrunacci: A side but useful note: when I move @OneToOne relation to the parent class Version, The error doesn't happen.
Author
Owner

@doctrinebot commented on GitHub (Oct 4, 2011):

Comment created by @guilhermeblanco:

Should be the Summary::$version pointing to Article::$summary.

Your mapping is wrong conceptually in OOP, because you're referring a non-existing property of Version ($summary is only part of Article).
Please fix the issue and check out if the error still persist.

@doctrinebot commented on GitHub (Oct 4, 2011): Comment created by @guilhermeblanco: Should be the Summary::$version pointing to Article::$summary. Your mapping is wrong conceptually in OOP, because you're referring a non-existing property of Version ($summary is only part of Article). Please fix the issue and check out if the error still persist.
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2011):

Comment created by @beberlei:

The mapping is wrong as guilherme said,

class Summary{
    /****
     * @OneToOne(targetEntity="Model\Lantern\Content\Article", inversedBy="summary")
     * @JoinColumn(name="version*id", referencedColumnName="version*id")
     */
    private $version;    
...
}
@doctrinebot commented on GitHub (Oct 15, 2011): Comment created by @beberlei: The mapping is wrong as guilherme said, ``` class Summary{ /**** * @OneToOne(targetEntity="Model\Lantern\Content\Article", inversedBy="summary") * @JoinColumn(name="version*id", referencedColumnName="version*id") */ private $version; ... } ```
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2011):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Oct 15, 2011): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1749