DDC-261: Object population from OneToOne association #322

Open
opened 2026-01-22 12:34:48 +01:00 by admin · 0 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:48 +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#322