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

  • SEARCH
  • Home
  • 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 7003921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:07:54+00:00 2026-05-27T21:07:54+00:00

I’m having this behavior with Doctrine 2.1 where I’m looking for a nice ‘workaround’.

  • 0

I’m having this behavior with Doctrine 2.1 where I’m looking for a nice ‘workaround’. The problem is as follows:

I have a user Entity:

/**
 * @Entity(repositoryClass="Application\Entity\Repository\UserRepository")
 * @HasLifecycleCallbacks
 */
class User extends AbstractEntity
{
    /**
     * 
     * @var integer
     * 
     * @Column(type="integer",nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * 
     * @var \DateTime
     * @Column(type="datetime",nullable=false)   
     */
    protected $insertDate;

    /**
     *
     * @var string
     * @Column(type="string", nullable=false)
     */
    protected $username;

    /**
     *
     * @ManyToOne(targetEntity="UserGroup", cascade={"merge"})
     */
    protected $userGroup;
}

And a usergroup entity:

/**
 * @Entity
 */
class UserGroup extends AbstractEntity
{
    /**
     * 
     * @var integer
     * 
     * @Column(type="integer",nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

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

If I instantiate a user object (doing this with Zend_Auth) and Zend_Auth puts it automatically the session.

The problem is however, that is I pull it back from the session at a next page then the data in the user class is perfectly loaded but not in the userGroup association. If I add cascade={“merge”} into the annotation in the user object the userGroup object IS loaded but the data is empty. If you dump something like:

$user->userGroup->name

You will get NULL back. The problem is no data of the usergroup entity is accesed before the user object is saved in the session so a empty initialized object will be returned. If I do something like:

echo $user->userGroup->name;

Before I store the user object in the session all data of the assocication userGroup is succesfully saved and won’t return NULL on the next page if I try to access the $user->userGroup->name variable.

Is there a simple way to fix this? Can I manually load the userGroup object/association with a lifecycle callback @onLoad in the user class maybe? Any suggestions?

  • 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-05-27T21:07:55+00:00Added an answer on May 27, 2026 at 9:07 pm

    Your problem is a combination of what mjh_ca answered and a problem with your AbstractEntity implementation.

    Since you show that you access entity fields in this fashion:

    $user->userGroup->name;
    

    I assume your AbstractEntity base class is using __get() and __set() magic methods instead of proper getters and setters:

    function getUserGroup()
    {
        return $this->userGroup;
    }
    
    function setUserGroup(UserGroup $userGroup)
    {
        $this->userGroup = $userGroup;
    }
    

    You are essentially breaking lazy loading:

    “… whenever you access a public property of a proxy object that hasn’t been initialized yet the return value will be null. Doctrine cannot hook into this process and magically make the entity lazy load.”

    Source: Doctrine Best Practices: Don’t Use Public Properties on Entities

    You should instead be accessing fields this way:

    $user->getUserGroup()->getName();
    

    The second part of your problem is exactly as mjh_ca wrote – Zend_Auth detaches your entity from the entity manager when it serializes it for storage in the session. Setting cascade={"merge"} on your association will not work because it is the actual entity that is detached. You have to merge the deserialized User entity into the entity manager.

    $detachedIdentity = Zend_Auth::getInstance()->getIdentity();
    $identity = $em->merge($detachedIdentity);
    

    The question, is how to do this cleanly. You could look into implementing a __wakeup() magic method for your User entity, but that is also against doctrine best practices…

    Source: Implementing Wakeup or Clone

    Since we are talking about Zend_Auth, you could extend Zend_Auth and override the getIdentity() function so that it is entity aware.

    use Doctrine\ORM\EntityManager,
        Doctrine\ORM\UnitOfWork;
    
    class My_Auth extends \Zend_Auth
    {
        protected $_entityManager;
    
        /**
         * override otherwise self::$_instance
         * will still create an instance of Zend_Auth
         */
        public static function getInstance()
        {
            if (null === self::$_instance) {
                self::$_instance = new self();
            }
    
            return self::$_instance;
        }
    
        public function getEntityManager()
        {
            return $this->_entityManager;
        }
    
        public function setEntityManager(EntityManager $entityManager)
        {
            $this->_entityManager = $entityManager;
        }
    
        public function getIdentity()
        {
            $storage = $this->getStorage();
    
            if ($storage->isEmpty()) {
                return null;
            }
    
            $identity = $storage->read();
    
            $em = $this->getEntityManager();
            if(UnitOfWork::STATE_DETACHED === $em->getUnitOfWork()->getEntityState($identity))
            {
                $identity = $em->merge($identity);
            }
    
            return $identity;
        }
    }
    

    And than add an _init function to your Bootstrap:

    public function _initAuth()
    {
        $this->bootstrap('doctrine');
        $em = $this->getResource('doctrine')->getEntityManager();
    
        $auth = My_Auth::getInstance();
        $auth->setEntityManager($em);
    }
    

    At this point calling $user->getUserGroup()->getName(); should work as intended.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into

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.