DDC-41: Getting error with lazy loading via createQuery() followed by $em->flush() #49

Closed
opened 2026-01-22 12:25:21 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 10, 2009).

Jira issue originally created by user crotalus:

Simple O-O relationship between \Entities\User and \Entities\Feed. Seems like there's a problem with not-yet lazy-loaded proxies and $em->flush().

<?php
namespace Entities;

/*** @Entity @Table(name="users_debug") **/
class User {
    /****
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /****
     * @OneToOne(targetEntity="Feed", mappedBy="User", cascade={"persist"})
     */
    private $Feed;

    public function getID() {
        return $this->id;
    }
    public function getFeed() {
        return $this->Feed;
    }
    public function setFeed($feed) {
        $this->Feed = $feed;
    }
}
?>

<?php
namespace Entities;

/****
 * @Entity @Table(name="feeds_debug")
 */
class Feed {
    /****
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO", allocationSize=1)
     */
    private $id;

     /****
     * @OneToOne(targetEntity="User", cascade={"persist"})
     * @JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $User;

    function setID($value) {
        $this->id = $value;
    }
    function getID() {
        return $this->id;
    }
    function getUser() {
        return $this->User;
    }
    function setUser($user) {
        $this->User = $user;
    }
}


?>

Table-data

users_debug:
 id
 361

feeds_debug:
 id  | user_id
 461 |     361

Code:

$user = $em->createQuery("SELECT u FROM Entities\User u WHERE u.id = 361")->getSingleResult();
print $user->getID(); // 361
// uncomment line below and it works
// print $user->getFeed()->getID();
$em->flush();

Error:
Warning: spl_object_hash() expects parameter 1 to be object, null given in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php on line 1010
Warning: spl_object_hash() expects parameter 1 to be object, null given in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php on line 566
Warning: ReflectionProperty::setValue() expects parameter 1 to be object, null given in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php on line 575
Warning: get_class() expects parameter 1 to be object, null given in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php on line 979
Fatal error: Uncaught exception 'ReflectionException' with message 'Class does not exist' in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadata.php:69
Stack trace:
#0 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadata.php(69): ReflectionClass->**construct(false)
#1 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(247): Doctrine\ORM\Mapping\ClassMetadata->**construct(false)
#2 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(177): Doctrine\ORM\Mapping\ClassMetadataFactory->_newClassMetadataInstance(false)
#3 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(115): Doctrine\ORM\Mapping\ClassMetadataFactory->_loadMetadata(false)
#4 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/EntityManager.php(212): Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor(false)
#5 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php(979): Doctrine\ORM\EntityManager->getClassMetadata in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadata.php on line 69

PostgreSQL log:
2009-10-10 16:32:58 CEST LOGG: execute pdo_stmt_000000000a283af8: SELECT u0_.id AS id0 FROM users_debug u0_ WHERE u0_.id = 361
2009-10-10 16:32:58 CEST LOGG: sats: DEALLOCATE pdo_stmt_000000000a283af8
2009-10-10 16:32:58 CEST LOGG: execute pdo_stmt_000000000a283af8: SELECT NEXTVAL('feeds_debug_id_seq')
2009-10-10 16:32:58 CEST LOGG: sats: DEALLOCATE pdo_stmt_000000000a283af8
2009-10-10 16:32:58 CEST LOGG: execute pdo_stmt_000000000a283af8: SELECT NEXTVAL('users_debug_id_seq')
2009-10-10 16:32:58 CEST LOGG: sats: DEALLOCATE pdo_stmt_000000000a283af8

Originally created by @doctrinebot on GitHub (Oct 10, 2009). Jira issue originally created by user crotalus: Simple O-O relationship between \Entities\User and \Entities\Feed. Seems like there's a problem with not-yet lazy-loaded proxies and $em->flush(). ``` <?php namespace Entities; /*** @Entity @Table(name="users_debug") **/ class User { /**** * @Id @Column(type="integer") * @GeneratedValue(strategy="AUTO") */ private $id; /**** * @OneToOne(targetEntity="Feed", mappedBy="User", cascade={"persist"}) */ private $Feed; public function getID() { return $this->id; } public function getFeed() { return $this->Feed; } public function setFeed($feed) { $this->Feed = $feed; } } ?> ``` ``` <?php namespace Entities; /**** * @Entity @Table(name="feeds_debug") */ class Feed { /**** * @Id @Column(type="integer") * @GeneratedValue(strategy="AUTO", allocationSize=1) */ private $id; /**** * @OneToOne(targetEntity="User", cascade={"persist"}) * @JoinColumn(name="user_id", referencedColumnName="id") */ private $User; function setID($value) { $this->id = $value; } function getID() { return $this->id; } function getUser() { return $this->User; } function setUser($user) { $this->User = $user; } } ?> ``` Table-data ``` users_debug: id 361 feeds_debug: id | user_id 461 | 361 ``` Code: ``` $user = $em->createQuery("SELECT u FROM Entities\User u WHERE u.id = 361")->getSingleResult(); print $user->getID(); // 361 // uncomment line below and it works // print $user->getFeed()->getID(); $em->flush(); ``` Error: Warning: spl_object_hash() expects parameter 1 to be object, null given in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php on line 1010 Warning: spl_object_hash() expects parameter 1 to be object, null given in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php on line 566 Warning: ReflectionProperty::setValue() expects parameter 1 to be object, null given in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php on line 575 Warning: get_class() expects parameter 1 to be object, null given in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php on line 979 Fatal error: Uncaught exception 'ReflectionException' with message 'Class does not exist' in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadata.php:69 Stack trace: #0 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadata.php(69): ReflectionClass->**construct(false) #1 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(247): Doctrine\ORM\Mapping\ClassMetadata->**construct(false) #2 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(177): Doctrine\ORM\Mapping\ClassMetadataFactory->_newClassMetadataInstance(false) #3 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(115): Doctrine\ORM\Mapping\ClassMetadataFactory->_loadMetadata(false) #4 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/EntityManager.php(212): Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor(false) #5 /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/UnitOfWork.php(979): Doctrine\ORM\EntityManager->getClassMetadata in /home/crotalus/src/Doctrine2-Dev/lib/Doctrine/ORM/Mapping/ClassMetadata.php on line 69 PostgreSQL log: 2009-10-10 16:32:58 CEST LOGG: execute pdo_stmt_000000000a283af8: SELECT u0_.id AS id0 FROM users_debug u0_ WHERE u0_.id = 361 2009-10-10 16:32:58 CEST LOGG: sats: DEALLOCATE pdo_stmt_000000000a283af8 2009-10-10 16:32:58 CEST LOGG: execute pdo_stmt_000000000a283af8: SELECT NEXTVAL('feeds_debug_id_seq') 2009-10-10 16:32:58 CEST LOGG: sats: DEALLOCATE pdo_stmt_000000000a283af8 2009-10-10 16:32:58 CEST LOGG: execute pdo_stmt_000000000a283af8: SELECT NEXTVAL('users_debug_id_seq') 2009-10-10 16:32:58 CEST LOGG: sats: DEALLOCATE pdo_stmt_000000000a283af8
admin added the Bug label 2026-01-22 12:25:21 +01:00
admin closed this issue 2026-01-22 12:25:21 +01:00
Author
Owner

@doctrinebot commented on GitHub (Oct 12, 2009):

Comment created by romanb:

Strange, all the warnings and errors and the stack trace rather indicate that the associated value is NULL and not a (not initialized) proxy object. I'm working on this and already found an issue to address but I'm still unable to exactly reproduce this. Will keep you updated. If you have any further information, please let me know.

@doctrinebot commented on GitHub (Oct 12, 2009): Comment created by romanb: Strange, all the warnings and errors and the stack trace rather indicate that the associated value is NULL and not a (not initialized) proxy object. I'm working on this and already found an issue to address but I'm still unable to exactly reproduce this. Will keep you updated. If you have any further information, please let me know.
Author
Owner

@doctrinebot commented on GitHub (Oct 12, 2009):

Comment created by romanb:

OK, managed to reproduce this. Working on it.

@doctrinebot commented on GitHub (Oct 12, 2009): Comment created by romanb: OK, managed to reproduce this. Working on it.
Author
Owner

@doctrinebot commented on GitHub (Oct 12, 2009):

Comment created by romanb:

This should now be fixed but you need to manually delete your proxy classes so that they're regenerated. More improvements to the proxy classes and CLI tasks for (re)generating proxy classes will follow.

@doctrinebot commented on GitHub (Oct 12, 2009): Comment created by romanb: This should now be fixed but you need to manually delete your proxy classes so that they're regenerated. More improvements to the proxy classes and CLI tasks for (re)generating proxy classes will follow.
Author
Owner

@doctrinebot commented on GitHub (Oct 12, 2009):

Issue was closed with resolution "Fixed"

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

No dependencies set.

Reference: doctrine/archived-orm#49