Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1300

Can't fetch entities from IdentityMap when using a foreign keys as identifiers

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: 2.1
    • Fix Version/s: 2.1.1
    • Component/s: ORM
    • Labels:
      None

      Description

      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.

        Activity

        Hide
        Benjamin Eberlei added a comment -

        I could reproduce this, working on a fix.

        Show
        Benjamin Eberlei added a comment - I could reproduce this, working on a fix.
        Hide
        Benjamin Eberlei added a comment -

        Fixed

        Show
        Benjamin Eberlei added a comment - Fixed

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Van Hauwaert Bert
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: