DDC-1300: Can't fetch entities from IdentityMap when using a foreign keys as identifiers #1633

Open
opened 2026-01-22 13:20:32 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 27, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user tbo:

Given the follow SQL structure:

CREATE TABLE `foo` (
  `fooID` int(11) NOT NULL AUTO_INCREMENT,
  `fooReference` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`fooID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='module=bug';

CREATE TABLE `fooLocale` (
  `fooID` int(11) NOT NULL,
  `locale` varchar(5) NOT NULL DEFAULT '',
  `title` varchar(150) DEFAULT NULL,
  PRIMARY KEY (`fooID`,`locale`),
  KEY `fk_foo2` (`fooID`),
  CONSTRAINT `fk*table1*foo2` FOREIGN KEY (`fooID`) REFERENCES `foo` (`fooID`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module=bug';

INSERT INTO `foo` (`fooID`, `fooReference`)
VALUES
    (1, 'foo1');

INSERT INTO `fooLocale` (`fooID`, `locale`, `title`)
VALUES
    (1, 'en_GB', 'Foo title test');

and the following models:

use Doctrine\ORM\Mapping as ORM;

/****
 * Model for foo
 *
 * @category Application
 * @package Bug
 * @subpackage Model
 * @ORM\Table(name="foo")
 * @ORM\Entity
 */
class Bug*Model*Foo
{
    /****
     * @var integer fooID
     * @ORM\Column(name="fooID", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Id
     */
    protected $_fooID = null;

    /****
     * @var string fooReference
     * @ORM\Column(name="fooReference", type="string", nullable=true, length=45)
     */
    protected $_fooReference = null;

    /****
     * @ORM\OneToMany(targetEntity="Bug*Model_FooLocale", mappedBy="*foo",
     * cascade={"persist"})
     */
    protected $_fooLocaleRefFoo = null;

    /****
     * Constructor
     *
     * @param array|Zend_Config|null $options
     * @return Bug*Model*Foo
     */
    public function **construct($options = null)
    {
        $this->_fooLocaleRefFoo = new \Doctrine\Common\Collections\ArrayCollection();
    }

}
use Doctrine\ORM\Mapping as ORM;

/****
 * Model for fooLocale
 *
 * @category Application
 * @package Bug
 * @subpackage Model
 * @ORM\Table(name="fooLocale")
 * @ORM\Entity
 */
class Bug*Model*FooLocale
{
    /****
     * @ORM\ManyToOne(targetEntity="Bug*Model*Foo")
     * @ORM\JoinColumn(name="fooID", referencedColumnName="fooID")
     * @ORM\Id
     */
    protected $_foo = null;

    /****
     * @var string locale
     * @ORM\Column(name="locale", type="string", nullable=false, length=5)
     * @ORM\Id
     */
    protected $_locale = null;

    /****
     * @var string title
     * @ORM\Column(name="title", type="string", nullable=true, length=150)
     */
    protected $_title = null;

}

Execute the following DQL 2 times:

$query = $entityManager->createQuery('
            SELECT 
                f, fl 
            FROM 
                Bug*Model*Foo f 
            JOIN 
                f._fooLocaleRefFoo fl');
        return $query->getResult();

The first time, you won't have any issues.
The second time, when the data will be fetched from the identityMap, you won't find it because there is something wrong with the identities and you will get the following warning

( ! ) Notice: Undefined index: _foo in /var/www/vhost/core/htdocs/externals/Doctrine/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php on line 217
Call Stack
#   Time    Memory  Function    Location
1   0.0002  337752  {main}( )   ../index.php:0
2   0.0848  7268724 Zend_Application->run( )    ../index.php:26
3   0.0848  7268724 Zend*Application_Bootstrap*Bootstrap->run( )    ../Application.php:366
4   0.0850  7268796 Zend*Controller*Front->dispatch( )  ../Bootstrap.php:97
5   0.0908  7900416 Zend*Controller_Dispatcher*Standard->dispatch( )    ../Front.php:954
6   0.0967  8209564 Zend*Controller*Action->dispatch( ) ../Standard.php:295
7   0.0967  8213912 Bug_IndexController->indexAction( ) ../Action.php:513
8   0.1936  11391900    Bug*Service*Bug->getFooLocaleList( )    ../IndexController.php:35
9   0.1936  11394088    Doctrine\ORM\AbstractQuery->getResult( )    ../Bug.php:41
10  0.1936  11394296    Doctrine\ORM\AbstractQuery->execute( )  ../AbstractQuery.php:392
11  0.1945  11397960    Doctrine\ORM\Internal\Hydration\AbstractHydrator->hydrateAll( ) ../AbstractQuery.php:594
12  0.1946  11398656    Doctrine\ORM\Internal\Hydration\ObjectHydrator->_hydrateAll( )  ../AbstractHydrator.php:99
13  0.1946  11399876    Doctrine\ORM\Internal\Hydration\ObjectHydrator->_hydrateRow( )  ../ObjectHydrator.php:140
14  0.1949  11405584    Doctrine\ORM\Internal\Hydration\ObjectHydrator->_getEntityFromIdentityMap( )    ../ObjectHydrator.php:326

With the invalid key, it is not possible to get the entity from the identityMap.

Originally created by @doctrinebot on GitHub (Jul 27, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user tbo: Given the follow SQL structure: ``` CREATE TABLE `foo` ( `fooID` int(11) NOT NULL AUTO_INCREMENT, `fooReference` varchar(45) DEFAULT NULL, PRIMARY KEY (`fooID`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='module=bug'; CREATE TABLE `fooLocale` ( `fooID` int(11) NOT NULL, `locale` varchar(5) NOT NULL DEFAULT '', `title` varchar(150) DEFAULT NULL, PRIMARY KEY (`fooID`,`locale`), KEY `fk_foo2` (`fooID`), CONSTRAINT `fk*table1*foo2` FOREIGN KEY (`fooID`) REFERENCES `foo` (`fooID`) ON DELETE CASCADE ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module=bug'; INSERT INTO `foo` (`fooID`, `fooReference`) VALUES (1, 'foo1'); INSERT INTO `fooLocale` (`fooID`, `locale`, `title`) VALUES (1, 'en_GB', 'Foo title test'); ``` and the following models: ``` use Doctrine\ORM\Mapping as ORM; /**** * Model for foo * * @category Application * @package Bug * @subpackage Model * @ORM\Table(name="foo") * @ORM\Entity */ class Bug*Model*Foo { /**** * @var integer fooID * @ORM\Column(name="fooID", type="integer", nullable=false) * @ORM\GeneratedValue(strategy="IDENTITY") * @ORM\Id */ protected $_fooID = null; /**** * @var string fooReference * @ORM\Column(name="fooReference", type="string", nullable=true, length=45) */ protected $_fooReference = null; /**** * @ORM\OneToMany(targetEntity="Bug*Model_FooLocale", mappedBy="*foo", * cascade={"persist"}) */ protected $_fooLocaleRefFoo = null; /**** * Constructor * * @param array|Zend_Config|null $options * @return Bug*Model*Foo */ public function **construct($options = null) { $this->_fooLocaleRefFoo = new \Doctrine\Common\Collections\ArrayCollection(); } } ``` ``` use Doctrine\ORM\Mapping as ORM; /**** * Model for fooLocale * * @category Application * @package Bug * @subpackage Model * @ORM\Table(name="fooLocale") * @ORM\Entity */ class Bug*Model*FooLocale { /**** * @ORM\ManyToOne(targetEntity="Bug*Model*Foo") * @ORM\JoinColumn(name="fooID", referencedColumnName="fooID") * @ORM\Id */ protected $_foo = null; /**** * @var string locale * @ORM\Column(name="locale", type="string", nullable=false, length=5) * @ORM\Id */ protected $_locale = null; /**** * @var string title * @ORM\Column(name="title", type="string", nullable=true, length=150) */ protected $_title = null; } ``` Execute the following DQL 2 times: ``` $query = $entityManager->createQuery(' SELECT f, fl FROM Bug*Model*Foo f JOIN f._fooLocaleRefFoo fl'); return $query->getResult(); ``` The first time, you won't have any issues. The second time, when the data will be fetched from the identityMap, you won't find it because there is something wrong with the identities and you will get the following warning ``` ( ! ) Notice: Undefined index: _foo in /var/www/vhost/core/htdocs/externals/Doctrine/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php on line 217 Call Stack # Time Memory Function Location 1 0.0002 337752 {main}( ) ../index.php:0 2 0.0848 7268724 Zend_Application->run( ) ../index.php:26 3 0.0848 7268724 Zend*Application_Bootstrap*Bootstrap->run( ) ../Application.php:366 4 0.0850 7268796 Zend*Controller*Front->dispatch( ) ../Bootstrap.php:97 5 0.0908 7900416 Zend*Controller_Dispatcher*Standard->dispatch( ) ../Front.php:954 6 0.0967 8209564 Zend*Controller*Action->dispatch( ) ../Standard.php:295 7 0.0967 8213912 Bug_IndexController->indexAction( ) ../Action.php:513 8 0.1936 11391900 Bug*Service*Bug->getFooLocaleList( ) ../IndexController.php:35 9 0.1936 11394088 Doctrine\ORM\AbstractQuery->getResult( ) ../Bug.php:41 10 0.1936 11394296 Doctrine\ORM\AbstractQuery->execute( ) ../AbstractQuery.php:392 11 0.1945 11397960 Doctrine\ORM\Internal\Hydration\AbstractHydrator->hydrateAll( ) ../AbstractQuery.php:594 12 0.1946 11398656 Doctrine\ORM\Internal\Hydration\ObjectHydrator->_hydrateAll( ) ../AbstractHydrator.php:99 13 0.1946 11399876 Doctrine\ORM\Internal\Hydration\ObjectHydrator->_hydrateRow( ) ../ObjectHydrator.php:140 14 0.1949 11405584 Doctrine\ORM\Internal\Hydration\ObjectHydrator->_getEntityFromIdentityMap( ) ../ObjectHydrator.php:326 ``` With the invalid key, it is not possible to get the entity from the identityMap.
admin added the Bug label 2026-01-22 13:20:32 +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#1633