How to select discriminator column in doctrine 2
Asked Answered
D

3

9

I need some help when select only discriminator column from doctrine 2 when run the DQL below

SELECT p.type FROM AppBundle\Entity\Product p

type is discriminator column in entity AppBundle\Entity\Product

@ORM\DiscriminatorColumn(name="type", type="smallint")`

@ORM\DiscriminatorMap({
    "0" = "AppBundle\Entity\Product",
    "1" = "AppBundle\Entity\Product\SingleIssue",
    "2" = "AppBundle\Entity\Product\CountBasedIssue",
    "3" = "AppBundle\Entity\Product\TimeBasedIssue"
})

I know that type is not a real property in entity, but is there anyway for me to do that?

Thanks in advance!

Updated

After 2 days for reading Doctrine codes, I decided to override SqlWalker and create new Hydrator by the snippets below

Override SqlWalker

<?php

namespace ...;

use Doctrine\ORM\Query\SqlWalker;

class CustomSqlWalker extends SqlWalker
{
    const FORCE_GET_DISCRIMINATOR_COLUMN = 'forceGetDiscriminatorColumn';
    const DISCRIMINATOR_CLASS_MAP = 'discriminatorClassMap';

    /**
     * {@inheritdoc}
     */
    public function walkSelectClause($selectClause)
    {
        $sql = parent::walkSelectClause($selectClause);
        $forceGetDiscriminatorColumn = $this->getQuery()->getHint(self::FORCE_GET_DISCRIMINATOR_COLUMN);
        if (empty($forceGetDiscriminatorColumn)) {
            return $sql;
        }

        foreach ($this->getQueryComponents() as $key => $queryComponent) {
            if (!in_array($key, $forceGetDiscriminatorColumn)) {
                continue;
            }

            $metadata = $queryComponent['metadata'];
            $discriminatorColumn = $metadata->discriminatorColumn['name'];
            $tableName = $metadata->table['name'];
            $tableAlias = $this->getSQLTableAlias($tableName, $key);
            $discriminatorColumnAlias = $this->getSQLColumnAlias($discriminatorColumn);
            $sql .= ", $tableAlias.$discriminatorColumn AS $discriminatorColumnAlias";
        }

        return $sql;
    }
}

Custom Hydrator

<?php

namespace ...;

use Doctrine\ORM\Internal\Hydration\ArrayHydrator;
use PDO;

class CustomHydrator extends ArrayHydrator
{
    /**
     * {@inheritdoc}
     */
    protected function hydrateAllData()
    {
        $result = array();

        $rootClassName = null;
        if (isset($this->_hints['forceGetDiscriminatorColumn']) &&
            isset($this->_hints['discriminatorClassMap'])) {
            $rootClassName = $this->_hints['discriminatorClassMap'];
        }

        while ($data = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
            foreach ($data as $key => $value) {
                if ($this->hydrateColumnInfo($key) != null ||
                    empty($rootClassName)) {
                    continue;
                }

                $metadata = $this->getClassMetadata($rootClassName);
                $discriminatorColumn = $metadata->discriminatorColumn;
                $fieldName = $discriminatorColumn['fieldName'];
                $type = $discriminatorColumn['type'];
                $this->_rsm->addScalarResult(
                    $key, $fieldName, $type
                );
            }
            $this->hydrateRowData($data, $result);
        }

        return $result;
    }
}

Configure custom hydrator

orm:
    ...
    hydrators:
        CustomHydrator: YourNamespace\To\CustomHydrator

Final step

$query = $queryBuilder->getQuery();
$query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'YourNamespace\To\CustomSqlWalker');
$query->setHint(\YourNamespace\To\CustomSqlWalker::FORCE_GET_DISCRIMINATOR_COLUMN, array($rootAlias)); // this alias will be used in CustomSqlWalker class
$query->setHint(\YourNamespace\To\CustomSqlWalker::DISCRIMINATOR_CLASS_MAP, $this->getClassName()); // this full-qualify class name will be used in CustomHydrator class

$products = $query->getResult('CustomHydrator');

TL;DR

I know this is a very complicated solution (may be just for my scenario), so I hope someone could give me another simple way to fix that, thanks so much!

Damick answered 27/10, 2015 at 4:55 Comment(1)
While MAtteo's use of INSTANCE OF seems to be the cleanest solution, my first thought was doubling the discriminator column, with one Doctrine wouldn't mask. Well, thats ugly and needs to ensure both columns can't desync, of course, but has the advantage of letting you do whatever you need with your data.Overcome
A
13

There is no direct access to the discriminator column.

It may happen that the entities of a special type should be queried. Because there is no direct access to the discriminator column, Doctrine provides the INSTANCE OF construct.

You can query for the type of your entity using the INSTANCE OF DQL as described in the docs. As example:

$query = $em->createQuery("SELECT product FROM AppBundle\Entity\AbstractProduct product WHERE product  INSTANCE OF AppBundle\Entity\Product");
$products = $query->getResult();

Hope this helps

Aundrea answered 27/10, 2015 at 8:9 Comment(1)
Yes, this is the right answer for dql direct retrieve. I've noticed that even in my project I use this kind of solution :)Excipient
E
8

I use this little "hack"

  1. Define a common interface for your entities (optional but recommended)
  2. Create a getType method in this interface
  3. Create constant into Discriminator entity
  4. Return the proper constant inside every discriminated entity

That way you can retrieve the discriminator "generic" entity (Product in your case) and call getType onto it.

Of course if you're interested into result filtering done directly by sql, this is not a solution at all and, I'm afraid, there isn't any solution available at the moment.
If you find one better that this, please share with us.

Excipient answered 27/10, 2015 at 7:49 Comment(1)
also abstract public function getType() works fine (my Discriminator entity is also abstract)Movie
H
3

You should be able to do this with a scalar result with INSTANCE OF and a case, when, (else,) end clause:

SELECT
  (case
     when p INSTANCE OF AppBundle\Entity\Product then \'0\'
     when p INSTANCE OF AppBundle\Entity\Product\SingleIssue then \'1\'
     when p INSTANCE OF AppBundle\Entity\Product\CountBasedIssue then \'2\'
     when p INSTANCE OF AppBundle\Entity\Product\TimeBasedIssue then \'3\'
     else \'foobar\'
   end) as type
FROM
  AppBundle\Entity\Product p

Of course the disadvantage is you have to update the query every time you add a DiscriminatorMap entry.

Halford answered 11/1, 2020 at 0:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.