Creating a index with invalid fields in the metadata does not report any error #7309

Open
opened 2026-01-22 15:49:42 +01:00 by admin · 1 comment
Owner

Originally created by @stof on GitHub (Feb 5, 2024).

Bug Report

Q A
BC Break no
Version 2.17.4

Summary

When the fields list in an Index contains fields that don't exist in the entity, they are silently ignored without any warning, creating an index containing only the other fields.

Current behavior

How to reproduce

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity()]
#[ORM\Index(fields: ['recipient', 'created_at', 'viewed'], name: 'bad')]
#[ORM\Index(fields: ['recipient', 'createdAt', 'viewed'], name: 'good')]
class Notification
{
    /**
     * @var int
     */
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private $id;

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private User $recipient;

    #[ORM\Column(type: 'datetimetz')]
    private \DateTime $createdAt;

    #[ORM\Column(type: 'boolean')]
    private bool $viewed = false;

    // ...
}
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity()]
class User
{
    /**
     * @var int
     */
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private $id;

    // ...
}

Expected behavior

Ideally, this should throw a MappingException. However, this is a BC break so it will need to wait until the next major version.

In the meantime, I see 2 possible improvements (not exclusive to each other):

  • make the SchemaValidator detect such mistake and report it as a validation error
  • add an opt-in configuration setting to turn on such stricter validation without waiting for the next major version
Originally created by @stof on GitHub (Feb 5, 2024). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 2.17.4 #### Summary When the `fields` list in an Index contains fields that don't exist in the entity, they are silently ignored without any warning, creating an index containing only the other fields. #### Current behavior <!-- What is the current (buggy) behavior? --> #### How to reproduce ```php <?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity()] #[ORM\Index(fields: ['recipient', 'created_at', 'viewed'], name: 'bad')] #[ORM\Index(fields: ['recipient', 'createdAt', 'viewed'], name: 'good')] class Notification { /** * @var int */ #[ORM\Id] #[ORM\Column(type: 'integer')] #[ORM\GeneratedValue(strategy: 'AUTO')] private $id; #[ORM\ManyToOne(targetEntity: User::class)] #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] private User $recipient; #[ORM\Column(type: 'datetimetz')] private \DateTime $createdAt; #[ORM\Column(type: 'boolean')] private bool $viewed = false; // ... } ``` ```php <?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity()] class User { /** * @var int */ #[ORM\Id] #[ORM\Column(type: 'integer')] #[ORM\GeneratedValue(strategy: 'AUTO')] private $id; // ... } ``` #### Expected behavior Ideally, this should throw a MappingException. However, this is a BC break so it will need to wait until the next major version. In the meantime, I see 2 possible improvements (not exclusive to each other): - make the SchemaValidator detect such mistake and report it as a validation error - add an opt-in configuration setting to turn on such stricter validation without waiting for the next major version
Author
Owner

@stof commented on GitHub (Feb 5, 2024):

Note that configuring columns (expecting database column names) instead of fields (expecting names of mapped properties) is properly throwing an exception in DBAL for invalid column names. My guess is that the invalid fields are silently skipped when converting field names to column names when creating the DBAL Schema.

@stof commented on GitHub (Feb 5, 2024): Note that configuring `columns` (expecting database column names) instead of `fields` (expecting names of mapped properties) is properly throwing an exception in DBAL for invalid column names. My guess is that the invalid fields are silently skipped when converting field names to column names when creating the DBAL Schema.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7309