DDC-3349: Possibility to override order of fields of composite ID produced by Mapping #4140

Closed
opened 2026-01-22 14:35:59 +01:00 by admin · 7 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 13, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user tiger-seo:

So, the problem is when the one needs to use association key in composite identifier; they are added in the end of the identifier array, which is clearly not always suitable in regards to performance.
For example, following mapping:

Acme\DemoBundle\Entity\PageLocalFans:
    type: entity
    id:
        date:
            type: date
        page:
            associationKey: true
        countryCode:
            type: string
            length: 2
    fields:
        fans:
            type: integer
    manyToOne:
        page:
            targetEntity: Page
            joinColumn:
                name: page_id
                referencedColumnName: id
                onDelete: CASCADE

will turn into sql as:

CREATE TABLE page*local*fans (
  date         DATE       NOT NULL,
  country_code VARCHAR(2) NOT NULL,
  page_id      INT        NOT NULL,
  fans         INT        NOT NULL,
  INDEX IDX*7391EB36C4663E4 (page*id),
  PRIMARY KEY (date, country*code, page*id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8*unicode*ci ENGINE = InnoDB;

and there is no way to change the order of the primary from PRIMARY KEY (date, country*code, page_id) to PRIMARY KEY (date, page_id, country*code)

Originally created by @doctrinebot on GitHub (Oct 13, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user tiger-seo: So, the problem is when the one needs to use association key in composite identifier; they are added in the end of the identifier array, which is clearly not always suitable in regards to performance. For example, following mapping: ``` Acme\DemoBundle\Entity\PageLocalFans: type: entity id: date: type: date page: associationKey: true countryCode: type: string length: 2 fields: fans: type: integer manyToOne: page: targetEntity: Page joinColumn: name: page_id referencedColumnName: id onDelete: CASCADE ``` will turn into sql as: ``` CREATE TABLE page*local*fans ( date DATE NOT NULL, country_code VARCHAR(2) NOT NULL, page_id INT NOT NULL, fans INT NOT NULL, INDEX IDX*7391EB36C4663E4 (page*id), PRIMARY KEY (date, country*code, page*id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8*unicode*ci ENGINE = InnoDB; ``` and there is no way to change the order of the primary from `PRIMARY KEY (date, country*code, page_id)` to `PRIMARY KEY (date, page_id, country*code)`
admin added the New FeatureWon't Fix labels 2026-01-22 14:35:59 +01:00
admin closed this issue 2026-01-22 14:36:00 +01:00
Author
Owner
@doctrinebot commented on GitHub (Oct 13, 2014): - relates to [DDC-3352: [GH-1162] DDC-3349: Possibility to override order of fields of composite ID produc...](http://www.doctrine-project.org/jira/browse/DDC-3352)
Author
Owner

@doctrinebot commented on GitHub (Oct 14, 2014):

Comment created by tiger-seo:

i've done the PR for this, pls see https://github.com/doctrine/doctrine2/pull/1162

@doctrinebot commented on GitHub (Oct 14, 2014): Comment created by tiger-seo: i've done the PR for this, pls see https://github.com/doctrine/doctrine2/pull/1162
Author
Owner

@doctrinebot commented on GitHub (Oct 17, 2014):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1162] was assigned:
https://github.com/doctrine/doctrine2/pull/1162

@doctrinebot commented on GitHub (Oct 17, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-1162] was assigned: https://github.com/doctrine/doctrine2/pull/1162
Author
Owner

@doctrinebot commented on GitHub (Jan 16, 2015):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1162] was closed:
https://github.com/doctrine/doctrine2/pull/1162

@doctrinebot commented on GitHub (Jan 16, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-1162] was closed: https://github.com/doctrine/doctrine2/pull/1162
Author
Owner

@leongersen commented on GitHub (Aug 5, 2019):

This issue can probably be closed. A PR for this issue was closed with the following suggestion:

consider using an event listener and re-arranging identifier order by directly manipulating the ClassMetadata instance when it is being loaded

For reference, such a listener would look like this:

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;

class DoctrineSchemaListener implements EventSubscriber
{
    public function getSubscribedEvents()
    {
        return [Events::loadClassMetadata];
    }

    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
    {
        $classMetadata = $eventArgs->getClassMetadata();

        if ($classMetadata->getName() === YourClass::class) {
            $classMetadata->setIdentifier(['date', 'page', 'country']);
        }
    }
}
@leongersen commented on GitHub (Aug 5, 2019): This issue can probably be closed. A PR for this issue was closed with the following suggestion: > consider using an event listener and re-arranging identifier order by directly manipulating the ClassMetadata instance when it is being loaded For reference, such a listener would look like this: ```php use Doctrine\Common\EventSubscriber; use Doctrine\ORM\Event\LoadClassMetadataEventArgs; use Doctrine\ORM\Events; class DoctrineSchemaListener implements EventSubscriber { public function getSubscribedEvents() { return [Events::loadClassMetadata]; } public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) { $classMetadata = $eventArgs->getClassMetadata(); if ($classMetadata->getName() === YourClass::class) { $classMetadata->setIdentifier(['date', 'page', 'country']); } } } ```
Author
Owner

@SenseException commented on GitHub (Aug 14, 2019):

@doctrine/doctrinecore Should this be the default solution or is this issue from 2015 still relevant for a PR?

@SenseException commented on GitHub (Aug 14, 2019): @doctrine/doctrinecore Should this be the default solution or is this issue from 2015 still relevant for a PR?
Author
Owner

@Ocramius commented on GitHub (Aug 24, 2019):

I'd say that the solution by @leongersen is the correct one: subtype overrides shouldn't need more ORM-specific changes, but rather metadata creation hooks 👍

@Ocramius commented on GitHub (Aug 24, 2019): I'd say that the solution by @leongersen is the correct one: subtype overrides shouldn't need more ORM-specific changes, but rather metadata creation hooks :+1:
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4140