Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8824527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:39:21+00:00 2026-06-14T06:39:21+00:00

i have a question about the correct way to handle doctrine’s lazy loading mechanism.

  • 0

i have a question about the correct way to handle doctrine’s lazy loading mechanism. I have an entity that references another one via @ManyToOne:

class Entity {
    ...
    /**
     * @ManyToOne(targetEntity="AnotherEntity")
     * @JoinColumn(name="anotherEntityId", referencedColumnName="id")
     */
    protected $anotherEntity;
    ...
}

class AnotherEntity {
    /**
     * @var integer $id
     * @Column(name="id", type="integer", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $someValue
     * @Column(name="someValue", type="string")
     */
    private $someValue;
    ...
}

Doctrine now generates a proxy for AnotherEntity that implements lazy loading on its getters:

public function getSomeValue()
{
    $this->__load();
    return parent::getSomeValue();
}

If I now have some instances of Entity that do not have a reference counterpart of AnotherEntity ($anotherEntityId is 0 or null). Then doctrine seems to generate ONE null-Object of AnotherEntity and makes the $anotherEntity variables of all Entity object point to it. The null-object is uninitialized since we want lazy loading.
I now get an EntityNotFoundException when I do this:

$entiy->getAnotherEntity()->getSomeValue();

That’s great! The AnotherEntity I access does not exist in the database and so I get this exception.
BUT when I do the same with the second Entity (and all others afterwards) I do not get this exception, since they are all pointing to the same instance of AnotherEntity and doctrine marked it as initialized in the first call. So now I’m accessing an object that does not exist logically but do not get any exception – I just get the uninitialized variable from getSomeValue(), e.g. an empty string.

Do you have any idea how I can make my application to recognize that the object does not exist instead of just accepting the empty value?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:39:23+00:00Added an answer on June 14, 2026 at 6:39 am

    I guess that’s a bug of doctrine, since the proxy is marked as initialized even if it is not when the EntityNotFoundException is thrown. Here is the code doctrine generates for each proxy:

    public function __load()
    {
        if (!$this->__isInitialized__ && $this->_entityPersister) {
            $this->__isInitialized__ = true;
    
            if (method_exists($this, "__wakeup")) {
                // call this after __isInitialized__to avoid infinite recursion
                // but before loading to emulate what ClassMetadata::newInstance()
                // provides.
                $this->__wakeup();
            }
    
            if ($this->_entityPersister->load($this->_identifier, $this) === null) {
                throw new \Doctrine\ORM\EntityNotFoundException();
            }
            unset($this->_entityPersister, $this->_identifier);
        }
    }
    

    In the file ProxyFactory you can find this code and by adding the following line just before the exception is thrown the problem is solved:

    $this->__isInitialized__ = false;
    

    With this you get the exception everytime it’s needed.

    Btw: the doctrine people already discussed that issue themselves but the fix does not seem to be in the current version: https://github.com/doctrine/doctrine2/pull/364

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about the correct way to handle errors in VBA in
I have a question about list in tcl, what is the correct way to
I have a question about how is the correct way of manipulate the initialization
I would like to ask a question about correct way of canceling socket operations
I have a question about righteous way of programming in Python... Maybe there can
I have a question regarding the correct way of splitting objects. Say I have
So I have two questions about HashMap s in Java: What is the correct
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.