2

How do I enable duplicate Discriminator Maps, allow a "default" mapping, or completely ignore Discriminator mappings when running a DQL query?

Setup:

// DQL Query:
$this->createQueryBuilder('s')
     ->select(['s.serverId', 'p.projectName'])
     ->leftJoin('s.serverServices', 'ss')
     ->leftJoin('ss.serverServiceProjects', 'ps')
     ->leftJoin('ps.project', 'p')
     ->getQuery()
     ->getArrayResult()

// Mapping
/**
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="service_type_id", type="integer")
 * @ORM\DiscriminatorMap({
 *     "1" = "AppBundle\Entity\ServerService",
 *     "2" = "AppBundle\Entity\ServerService",
 *       [...]
 *     "12" = "AppBundle\Entity\Service\SubService",
 *     "" = "AppBundle\Entity\ServerService"
 * })
 */

The join portion that's generated:

   LEFT JOIN server_services s2_ ON s0_.server_id = s2_.server_id AND s2_.service_type_id IN ('1', '12') 

This is entirely incorrect. I have an entire mapping of 1 - 12. And an "" empty string match. If a row does not have a known mapping (in my code) OR if it's empty, it should be set to the default base ServerService.

I did find this post: Leave out discriminator part of Doctrine' generated SQL

However, it's back in 2014, made mention of a difference in version, and does not compensate for duplicate, or default Mappings.

EDIT: For the record, I tried the ignore route -- it didn't work.

   $q->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, MySqlWalker::class)
        ->setHint(MySqlWalker::IGNORE_DISCRIMINATION, array('ss'));

Still returns the above SQL Joint statement. It does call MySqlWalker; it does go into the setInheritanceType() call, but it does not ignore the Discriminator mapping. :/

guice
  • 976
  • 4
  • 11
  • 31

1 Answers1

0

Found an answer to one option: Ignoring

In Doctrine 2.2.x, my statements were in the JOIN, not WHERE clause. Using the example in Leave out discriminator part of Doctrine' generated SQL, I had to overload the ->walkJoin() method to get ignore to work properly:

/**
 * {@inheritdoc}
 */
public function walkJoin($join)
{
    $this->checkForHint();
    return parent::walkJoin($join);
}

protected function checkForHint()
{
    $ignoreDescription = $this->getQuery()->getHint(self::IGNORE_DISCRIMINATION);

    if ($ignoreDescription) {
        foreach ($this->getQueryComponents() as $k => $component) {
            if (in_array($k, $ignoreDescription)) {
                /** @var $meta ClassMetadata */
                $meta = $component['metadata'];
                $meta->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_NONE);
            }
        }
    }
}

Still have not resolved Duplicate or Default discriminator values...

guice
  • 976
  • 4
  • 11
  • 31
  • Have you tried not declaring a `DiscriminatorMap` altogether? Default handling should cover all your use cases. Including when dealing with *ToOne and *ToMany and extended classes.[example](https://stackoverflow.com/a/45015357/1155833) – rkeet May 14 '18 at 19:42
  • That only works because names match classes. When using numeric discriminators, Doctrine cannot automatically decipher the right class. – guice May 15 '18 at 19:59
  • Hmmm I cannot remember which of the questions I was commenting and answering, but it was in the past 2 months. I looked into Doctrine's handling of Discriminator declaration, there I noticed that they've only used it [discriminator] in string representation. Thus, numeric was not fully (if at all) supported. At least, I think it was discriminators I was looking into at the time. Right now it's time for bed though, so if you're up, feel free to check out my activity related to Doctrine, should be in the past 2 months. – rkeet May 15 '18 at 20:52
  • quick look, [found it](https://stackoverflow.com/questions/49816728/doctrine-multi-level-inheritance-not-hydrate#comment86701034_49816728) though doesnt really explain, sorry. More info might be in [this old answer](https://stackoverflow.com/questions/45015356/zf2-doctrine-2-child-level-discriminators-with-class-table-inheritance/45015357#45015357) of mine – rkeet May 15 '18 at 20:56