Skip to content

Fix Doctrine ORM 4.0 deprecation: Replace ArrayAccess with direct property access on FieldMapping #491

@HecFranco

Description

@HecFranco

Description

The DoctrineORMListener in Lexik Translation Bundle is using ArrayAccess to access Doctrine\ORM\Mapping\FieldMapping properties, which triggers a deprecation warning. This approach will be removed in Doctrine ORM 4.0.

Error Details

Deprecation Message:

User Deprecated: Using ArrayAccess on Doctrine\ORM\Mapping\FieldMapping is deprecated and will not be possible in Doctrine ORM 4.0. Use the corresponding property instead.

Affected File:
Lexik\Bundle\TranslationBundle\Storage\Listener\DoctrineORMListener.php (line 28)

Stack Trace:

/usr/src/app/symfony/vendor/doctrine/deprecations/src/Deprecation.php:208
Doctrine\Deprecations\Deprecation::delegateTriggerToBackend(string $message, array $backtrace, string $link, string $package): void

/usr/src/app/symfony/vendor/doctrine/deprecations/src/Deprecation.php:108
Doctrine\Deprecations\Deprecation::trigger(string $package, string $link, string $message, ...$args): void

/usr/src/app/symfony/vendor/doctrine/orm/src/Mapping/ArrayAccessImplementation.php:18
Doctrine\ORM\Mapping\FieldMapping->offsetExists(mixed $offset): bool

/usr/src/app/symfony/vendor/lexik/translation-bundle/Storage/Listener/DoctrineORMListener.php:28
Lexik\Bundle\TranslationBundle\Storage\Listener\DoctrineORMListener->loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)

Steps to Reproduce

  1. Use Lexik Translation Bundle in a Symfony project with Doctrine ORM 3.x
  2. Enable deprecation warnings (or have them logged)
  3. Load any entity metadata that triggers the DoctrineORMListener::loadClassMetadata() method
  4. The deprecation warning will be triggered

Current Code (Problematic)

In Storage/Listener/DoctrineORMListener.php around line 28:

if (isset($fieldMapping['type']) && 'string' === $fieldMapping['type']) {
    $fieldMapping['length'] = 191;
}

Proposed Solution

Replace array access with direct property access. The getFieldMapping() method returns a FieldMapping object, so we should access its properties directly.

Simple Fix

Replace:

if (isset($fieldMapping['type']) && 'string' === $fieldMapping['type']) {
    $fieldMapping['length'] = 191;
}

With:

if (isset($fieldMapping->type) && 'string' === $fieldMapping->type) {
    $fieldMapping->length = 191;
}

Complete Method Example

Here's how the loadClassMetadata method should look after the fix (assuming the context around line 28):

public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
    $classMetadata = $eventArgs->getClassMetadata();
    
    // ... existing code ...
    
    foreach ($classMetadata->getFieldNames() as $field) {
        $fieldMapping = $classMetadata->getFieldMapping($field);
        
        // Fixed: Use property access instead of ArrayAccess
        if (isset($fieldMapping->type) && 'string' === $fieldMapping->type) {
            $fieldMapping->length = 191;
        }
    }
    
    // ... rest of the method ...
}

Notes

  • The FieldMapping class has public properties that can be accessed directly
  • Using isset($fieldMapping->type) checks if the property exists
  • Assigning $fieldMapping->length = 191 directly modifies the object property
  • This approach is compatible with Doctrine ORM 3.x and will work in 4.0

Expected Behavior

  • No deprecation warnings should be triggered
  • The functionality should remain the same (setting string field length to 191 for MySQL compatibility)
  • The code should be compatible with Doctrine ORM 4.0

Environment

  • Lexik Translation Bundle Version: (check your installed version)
  • Doctrine ORM Version: 3.x (deprecation present), 4.0 (will break)
  • PHP Version: (your PHP version)
  • Symfony Version: (your Symfony version)

References

Additional Notes

This deprecation is part of Doctrine ORM's migration away from ArrayAccess implementation on mapping classes. All mapping classes (FieldMapping, AssociationMapping, etc.) should now be accessed as objects with properties rather than arrays.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions