The discriminator column "key" is missing - initialize collection #6417

Open
opened 2026-01-22 15:32:52 +01:00 by admin · 4 comments
Owner

Originally created by @ybenhssaien on GitHub (Mar 2, 2020).

Bug Report

Q A
BC Break no
Version 2.7

Summary

Filter on a collection of entities with a reserved MySQL word as Mapping column generates an exception when initializing the collection after calling filter.

Current behavior

image

How to reproduce

Context
  • Two database tables : mission+ mission_atea
  • Three Entities : Mission (for table mission) + MissionAtea (for table mission_atea) + MissionAteaCommentaire (inherits from MissionAtea with discrimnation column key)
  • Relations : OneToMany (from Mission to MissionAtea) [attribut : extraData)
/**
 * @ORM\Table(name="mission_atea")
 * @ORM\Entity
 *            
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="`key`", type="string", length=100)
 * @ORM\DiscriminatorMap({"commentaire" = "App\Entity\Mission\MissionCommentaire", "*" = "MissionAtea"})
 */
class MissionAtea
{
.....
class Mission {
   /**
     * @var Collection
     *
     * @ORM\OneToMany(targetEntity="MissionAtea", mappedBy="mission", cascade={"persist", "remove"}, orphanRemoval=true)
     * @ORM\JoinColumn(name="id", referencedColumnName="id_mission")
     */
    private $extraData;

   public function getExtraDataByKey(string $key): ?MissionAtea
    {
        return $this->extraData->filter(
            function ($extra) use ($key) {
                /** @var MissionAtea $extra */
                return $extra->getKey() == $key;
        })->current() ?: null;
        }
    }

Database :
image

Call Mission::getExtraDataByKey('commentaire')

Expected behavior

Return the mapped entities (MissionAteaCommentaire)

Originally created by @ybenhssaien on GitHub (Mar 2, 2020). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.7 #### Summary Filter on a collection of entities with a reserved MySQL word as Mapping column generates an exception when initializing the collection after calling filter. #### Current behavior ![image](https://user-images.githubusercontent.com/7301643/75688204-5db63880-5c9f-11ea-8373-0404f0812ae6.png) #### How to reproduce ##### Context - Two database tables : `mission`+ `mission_atea` - Three Entities : `Mission` (for table mission) + `MissionAtea` (for table mission_atea) + `MissionAteaCommentaire` (inherits from MissionAtea with discrimnation column `key`) - Relations : OneToMany (from `Mission` to `MissionAtea`) [attribut : extraData) ```php /** * @ORM\Table(name="mission_atea") * @ORM\Entity * * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="`key`", type="string", length=100) * @ORM\DiscriminatorMap({"commentaire" = "App\Entity\Mission\MissionCommentaire", "*" = "MissionAtea"}) */ class MissionAtea { ..... ``` ```php class Mission { /** * @var Collection * * @ORM\OneToMany(targetEntity="MissionAtea", mappedBy="mission", cascade={"persist", "remove"}, orphanRemoval=true) * @ORM\JoinColumn(name="id", referencedColumnName="id_mission") */ private $extraData; public function getExtraDataByKey(string $key): ?MissionAtea { return $this->extraData->filter( function ($extra) use ($key) { /** @var MissionAtea $extra */ return $extra->getKey() == $key; })->current() ?: null; } } ``` Database : ![image](https://user-images.githubusercontent.com/7301643/75690692-95bf7a80-5ca3-11ea-9bbb-d2b9ff3e5779.png) Call `Mission::getExtraDataByKey('commentaire')` #### Expected behavior Return the mapped entities (MissionAteaCommentaire)
Author
Owner

@ybenhssaien commented on GitHub (Mar 2, 2020):

Problem source : Checking the existence of key in hydrateColumnInfo() method on AbstractHydrator class while storing `key` instead :

image

@ybenhssaien commented on GitHub (Mar 2, 2020): Problem source : Checking the existence of `key` in `hydrateColumnInfo()` method on `AbstractHydrator` class while storing <code>\`key\`</code> instead : ![image](https://user-images.githubusercontent.com/7301643/75694560-01581680-5ca9-11ea-9ce8-6515a4ec5c46.png)
Author
Owner

@beberlei commented on GitHub (Mar 2, 2020):

My first comment was wrong, this looks like indeed quoted discriminator columns is not working.

Honestly I don't see us fixing this anytime soon, because its probably more complex work. What you can do is see ClassMetadataInfo how field mapping and other tihngs set a booelan quoted = true when they detect the escape character being used and then other places using the ClassMetadataInfo::$discriminatorColumn for SQL generation to check for quoted.

It seems QuoteStrategy has no support for discriminator columns either, this would make sense as well to add.

All in all, your best solution would be not to use an SQL keyword as disciminrator column name. Hopefully thats possible.

@beberlei commented on GitHub (Mar 2, 2020): My first comment was wrong, this looks like indeed quoted discriminator columns is not working. Honestly I don't see us fixing this anytime soon, because its probably more complex work. What you can do is see `ClassMetadataInfo` how field mapping and other tihngs set a booelan `quoted = true` when they detect the escape character being used and then other places using the `ClassMetadataInfo::$discriminatorColumn` for SQL generation to check for quoted. It seems `QuoteStrategy` has no support for discriminator columns either, this would make sense as well to add. All in all, your best solution would be not to use an SQL keyword as disciminrator column name. Hopefully thats possible.
Author
Owner

@ybenhssaien commented on GitHub (Mar 3, 2020):

Thanks @beberlei

Actually is mentionned in the documentation : https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/basic-mapping.html#quoting-reserved-words

Identifier Quoting does not work for join column names or discriminator column names unless you are using a custom QuoteStrategy.

But, using this strategy, impact the generated select statement as the concatenation is done one the quoted column SELECT ..... , a3_.`key` AS `key`_70, .. FROM :
image

@ybenhssaien commented on GitHub (Mar 3, 2020): Thanks @beberlei Actually is mentionned in the documentation : https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/basic-mapping.html#quoting-reserved-words > Identifier Quoting does not work for join column names or discriminator column names unless you are using a custom QuoteStrategy. But, using this strategy, impact the generated select statement as the concatenation is done one the quoted column <code>SELECT ..... , a3_.\`key\` AS \`key\`_70, .. FROM</code> : ![image](https://user-images.githubusercontent.com/7301643/75757061-f1d0e000-5d31-11ea-8c98-688194b0bde7.png)
Author
Owner

@ybenhssaien commented on GitHub (Mar 3, 2020):

Everyone who is wondering about how this is done on Symfony > 4.0 app, just edit the config/packages/doctrine.yaml

doctrine:
    .....
    orm:
        quote_strategy: doctrine.orm.quote_strategy.ansi
@ybenhssaien commented on GitHub (Mar 3, 2020): Everyone who is wondering about how this is done on Symfony > 4.0 app, just edit the `config/packages/doctrine.yaml` ```yaml doctrine: ..... orm: quote_strategy: doctrine.orm.quote_strategy.ansi ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6417