DDC-261: Object population from OneToOne association #324

Closed
opened 2026-01-22 12:34:51 +01:00 by admin · 7 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 18, 2010).

Jira issue originally created by user fabrizzio:

Bugreport cfr. http://groups.google.com/group/doctrine-user/browse_thread/thread/51f5c14377c40e3b

I have a OneToOne relationship defined between a Page and a PageTemplate entity:

class Page
{
    /****
     * @Id
     * @GeneratedValue (strategy="AUTO")
     * @Column (type="integer")
     * 
     * @var int
     */
    protected $id;

    /****
     * @OneToOne(targetEntity="PageTemplate")
     * @JoinColumn(name="templateId", referencedColumnName="templateId")
     *
     * @var PageTemplate
     */
    protected $template;
}

class PageTemplate
{
    /****
     * @Id
     * @GeneratedValue (strategy="AUTO")
     * @Column (type="integer")
     *
     * @var int
     */
    protected $templateId;

    /****
     * @Column (type="string", length="255")
     *
     * @var string
     */
    protected $name;
}

When I try to fetch the Page and it's fully populated associated PageTemplate from the database, I get differect results using different Hydration modes (HYDRATE_OBJECT & HYDRATE_ARRAY):

When using:

$qb = $this->_em->createQueryBuilder();
$q = $qb->select('p', 'tpl')
                ->innerJoin('p.template', 'tpl')
                ->from('Entities\Fratello\Page', 'p')
                ->where($qb->expr()->eq('p.id', $id));

return $q->getQuery()->getSingleResult(); // using HYDRATE_OBJECT

... I get the following:

object(Entities\Fratello\Page)[438]
  protected 'id' => int 1
  protected 'template' => 
    object(Entities\Fratello\PageTemplate)[475]
      protected 'templateId' => null
      protected 'name' => null

On the other hand when I'm using HYDRATE_ARRAY:

$qb = $this->_em->createQueryBuilder();
$q = $qb->select('p', 'tpl')
                ->innerJoin('p.template', 'tpl')
                ->from('Entities\Fratello\Page', 'p')
                ->where($qb->expr()->eq('p.id', $id));

return $q->getQuery()->getSingleResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);

.. I get the following:

array
  'id' => int 1
  'template' => &
    array
      'templateId' => int 1
      'name' => string 'template 1' (length=10)

So fetch the object using HYDRATE_ARRAY returns the data I asked for, HYDRATE_OBJECT doesn't (returns a null object)

Originally created by @doctrinebot on GitHub (Jan 18, 2010). Jira issue originally created by user fabrizzio: Bugreport cfr. http://groups.google.com/group/doctrine-user/browse_thread/thread/51f5c14377c40e3b I have a OneToOne relationship defined between a Page and a PageTemplate entity: ``` class Page { /**** * @Id * @GeneratedValue (strategy="AUTO") * @Column (type="integer") * * @var int */ protected $id; /**** * @OneToOne(targetEntity="PageTemplate") * @JoinColumn(name="templateId", referencedColumnName="templateId") * * @var PageTemplate */ protected $template; } class PageTemplate { /**** * @Id * @GeneratedValue (strategy="AUTO") * @Column (type="integer") * * @var int */ protected $templateId; /**** * @Column (type="string", length="255") * * @var string */ protected $name; } ``` When I try to fetch the Page and it's fully populated associated PageTemplate from the database, I get differect results using different Hydration modes (HYDRATE_OBJECT & HYDRATE_ARRAY): When using: ``` $qb = $this->_em->createQueryBuilder(); $q = $qb->select('p', 'tpl') ->innerJoin('p.template', 'tpl') ->from('Entities\Fratello\Page', 'p') ->where($qb->expr()->eq('p.id', $id)); return $q->getQuery()->getSingleResult(); // using HYDRATE_OBJECT ``` ... I get the following: ``` object(Entities\Fratello\Page)[438] protected 'id' => int 1 protected 'template' => object(Entities\Fratello\PageTemplate)[475] protected 'templateId' => null protected 'name' => null ``` On the other hand when I'm using HYDRATE_ARRAY: ``` $qb = $this->_em->createQueryBuilder(); $q = $qb->select('p', 'tpl') ->innerJoin('p.template', 'tpl') ->from('Entities\Fratello\Page', 'p') ->where($qb->expr()->eq('p.id', $id)); return $q->getQuery()->getSingleResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY); ``` .. I get the following: ``` array 'id' => int 1 'template' => & array 'templateId' => int 1 'name' => string 'template 1' (length=10) ``` So fetch the object using HYDRATE_ARRAY returns the data I asked for, HYDRATE_OBJECT doesn't (returns a null object)
admin added the Bug label 2026-01-22 12:34:51 +01:00
admin closed this issue 2026-01-22 12:34:52 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 18, 2010):

@doctrinebot commented on GitHub (Jan 18, 2010): - depends on [DDC-79: Allow constructor arguments in persistable models](http://www.doctrine-project.org/jira/browse/DDC-79)
Author
Owner

@doctrinebot commented on GitHub (Jan 18, 2010):

Comment created by romanb:

Tried to reproduce it without success. See the attached testcase. Using latest code from trunk.

Please let us know what needs to be done in that testcase to reproduce your problem. Or provide your own testcase if you have one.

Thanks!

@doctrinebot commented on GitHub (Jan 18, 2010): Comment created by romanb: Tried to reproduce it without success. See the attached testcase. Using latest code from trunk. Please let us know what needs to be done in that testcase to reproduce your problem. Or provide your own testcase if you have one. Thanks!
Author
Owner

@doctrinebot commented on GitHub (Jan 18, 2010):

Comment created by fabrizzio:

I found the problem in my code. In the Page object constructor I composed an empty PageTemplate instance (null object):

class Page
{
    public function **construct()
    {
        // page template
        $this->template = new PageTemplate();
    }
}

This overrides the PageTemplate object that should be loaded.

Shouldn't the PageTemplate data be loaded after the Page constructor has been executed so it would overrule that one?

Thank you for your time!

@doctrinebot commented on GitHub (Jan 18, 2010): Comment created by fabrizzio: I found the problem in my code. In the Page object constructor I composed an empty PageTemplate instance (null object): ``` class Page { public function **construct() { // page template $this->template = new PageTemplate(); } } ``` This overrides the PageTemplate object that should be loaded. Shouldn't the PageTemplate data be loaded after the Page constructor has been executed so it would overrule that one? Thank you for your time!
Author
Owner

@doctrinebot commented on GitHub (Jan 18, 2010):

Comment created by romanb:

You are right, this is not correct. While this could be fixed in isolation I think it is better to fix it implicity by DDC-79 (not calling the constructor on reconstitution of persistent objects).

@doctrinebot commented on GitHub (Jan 18, 2010): Comment created by romanb: You are right, this is not correct. While this could be fixed in isolation I think it is better to fix it implicity by [DDC-79](http://www.doctrine-project.org/jira/browse/DDC-79) (not calling the constructor on reconstitution of persistent objects).
Author
Owner

@doctrinebot commented on GitHub (Feb 10, 2010):

Comment created by romanb:

This should now work without problems due to DDC-79.

@doctrinebot commented on GitHub (Feb 10, 2010): Comment created by romanb: This should now work without problems due to [DDC-79](http://www.doctrine-project.org/jira/browse/DDC-79).
Author
Owner

@doctrinebot commented on GitHub (Feb 10, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Feb 10, 2010): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2015):

Imported 1 attachments from Jira into https://gist.github.com/abb29d35fca7c9483666

@doctrinebot commented on GitHub (Dec 13, 2015): Imported 1 attachments from Jira into https://gist.github.com/abb29d35fca7c9483666 - [10296_DDC261Test.php](https://gist.github.com/abb29d35fca7c9483666#file-10296_DDC261Test-php)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#324