Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2543

EntityManager#getReference returns NULL on JOINED inheritance.

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 2.3.4
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:

      Description

      The following line returns null, when it shouldn't:

      SomeService.php
          $em->getReference('app:PessoaJuridica', 3);
      

      PessoaJurica inherits from Pessoa. Here goes the mapping.

      Pessoa.php
       /**
       * Pessoa
       *
       * @Table(name="pessoa")
       * @Entity(repositoryClass="Sicneo\Database\Repository\Pessoa")
       * @InheritanceType("JOINED")
       * @DiscriminatorColumn(name="tipo", type="string")
       * @DiscriminatorMap({
       *     "Física" = "PessoaFisica",
       *     "Jurídica" = "PessoaJuridica",
       *     "Internacional" = "PessoaInternacional"
       * })
       * @copyright Copyright (c) 2012 Funarbe Inc. (http://www.funarbe.org.br)
       */
      class Pessoa
      {
          ...
      }
      
      PessoaJuridica.php
      /**
       * PessoaJuridica
       *
       * @Table(name="pessoajuridica")
       * @Entity(repositoryClass="Sicneo\Database\Repository\PessoaJuridica")
       *
       * @copyright Copyright (c) 2012 Funarbe Inc. (http://www.funarbe.org.br)
       */
      class PessoaJuridica extends Pessoa
      {
          ...
      }
      

      The thing is: both 'Pessoa' and 'PessoaJuridica' with ID=3 exist on the database. Pessoa.tipo = "Jurídica". Everything seems to be correctly set up, but getReference keeps returning null.

      These are my tables:

      mysql> SELECT * FROM pessoa;

      id nome tipo ativo dtregistro
      1 Bruno Física 1 2013-01-01 00:00:00
      2 Gestor teste Jurídica 1 2013-01-01 00:00:00
      3 Coordenador teste Jurídica 1 2013-01-01 00:00:00
      4 Pessoa Internacional Internacional 1 2013-01-01 00:00:00
      5 Empresa Jurídica 1 2013-01-01 00:00:00

      5 rows in set (0.00 sec)

      mysql> SELECT id, cnpj FROM pessoajuridica;

      id cnpj
      3 33153182000190
      5 88302137000141
      2 99276220000100

      3 rows in set (0.00 sec)

      More info:

      $em->getRepository("app:Pessoa")->find(3); //returns a PessoaJuridica entity, as it should.

      and

      $em->getRepository("app:PessoaJuridica")->find(3); // returns null. No idea why.

        Activity

        Hide
        Marco Pivetta added a comment -

        Did you validate your mappings first? Also, is `PessoaJuridica` a leaf of your inheritance?

        Show
        Marco Pivetta added a comment - Did you validate your mappings first? Also, is `PessoaJuridica` a leaf of your inheritance?
        Hide
        Pedro Cordeiro added a comment -

        Symfony's profiler tells me there are no invalid mappings. Also, as you can see from the entities above, PessoaJuridica inherits from Pessoa and is defined on the DiscriminatorMap. It is a leaf. Everything seems to be consistent, but I keep getting null when trying to fetch the entity (from ->find(3)) or its proxies (from ->getRereference).

        Show
        Pedro Cordeiro added a comment - Symfony's profiler tells me there are no invalid mappings. Also, as you can see from the entities above, PessoaJuridica inherits from Pessoa and is defined on the DiscriminatorMap. It is a leaf. Everything seems to be consistent, but I keep getting null when trying to fetch the entity (from ->find(3)) or its proxies (from ->getRereference).
        Hide
        Marco Pivetta added a comment -

        Pedro Cordeiro what about the logged SQL ? Any queries being run?

        Show
        Marco Pivetta added a comment - Pedro Cordeiro what about the logged SQL ? Any queries being run?
        Hide
        Pedro Cordeiro added a comment -

        My logging is disabled. I'm not really running the project with symfony, I'm running with Zend Framework 1. I only imported the mappings to a symfony project and called an entity on the controller so I could have my mappings validated. The error occurs on a real life scenario, on ZF1. I have no SQL loggers there.

        Show
        Pedro Cordeiro added a comment - My logging is disabled. I'm not really running the project with symfony, I'm running with Zend Framework 1. I only imported the mappings to a symfony project and called an entity on the controller so I could have my mappings validated. The error occurs on a real life scenario, on ZF1. I have no SQL loggers there.
        Hide
        Marco Pivetta added a comment -

        Pedro Cordeiro can you attach an `EchoSQLLogger` to your connection and see if the query is going through and if it's then a problem in SQL or in the UnitOfWork?

        https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Logging/EchoSQLLogger.php

        Show
        Marco Pivetta added a comment - Pedro Cordeiro can you attach an `EchoSQLLogger` to your connection and see if the query is going through and if it's then a problem in SQL or in the UnitOfWork? https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Logging/EchoSQLLogger.php
        Hide
        Pedro Cordeiro added a comment -

        There was no bug. After much debugging, I figured out the problem. Something was messing with the tables, causing the issue.

        Show
        Pedro Cordeiro added a comment - There was no bug. After much debugging, I figured out the problem. Something was messing with the tables, causing the issue.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Pedro Cordeiro
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: