Skip to content

Read the id property of objects in ea_as_string - #7893

Merged
javiereguiluz merged 1 commit into
EasyCorp:5.xfrom
phcorp:fix_as_string_id_property
Nov 8, 2026
Merged

javiereguiluz merged 1 commit into
EasyCorp:5.xfrom
phcorp:fix_as_string_id_property

Conversation

@phcorp

@phcorp phcorp commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

ea_as_string shows an object that is neither translatable nor Stringable as <class> #<id>, and reads the id only through getId(). An entity can expose its id as a public property instead and have no getId() at all, for example with PHP 8.4 asymmetric visibility:

#[ORM\Entity]
class Book
{
    #[ORM\Id, ORM\GeneratedValue, ORM\Column]
    public private(set) ?int $id = null;
}

Such an entity is shown with an object hash that changes on every request, e.g. in the headers of CollectionField entries.

Also, when getId() returns something that can't be cast to a string (an array, or an object that isn't Stringable), the filter fails with "Object of class X could not be converted to string". When it returns null, it shows <class> #.

Fix

The id is read from getId(), or else from a public, initialized $id property (property hooks included). The object hash is still the fallback when there's no id, or when the id is null, an array or a non-Stringable object.

Tests

New cases in the ea_as_string data provider of tests/Unit/Twig/EasyAdminTwigExtensionTest.php:

  • a public $id property: an int, and a Stringable object
  • a private $id property: falls back to the hash
  • getId() returning null, and getId() returning a non-Stringable object: both fall back to the hash

Results:

  • make tests ARGS="tests/Unit/Twig": OK (287 tests)
  • make tests-parallel: OK (3691 tests)
  • phpstan and php-cs-fixer: no errors

@phcorp
phcorp force-pushed the fix_as_string_id_property branch 2 times, most recently from 1705cc2 to 5bc6c64 Compare November 7, 2026 02:06
The ea_as_string filter displays an object that is neither translatable
nor Stringable as "<class> #<id>", reading the id only from getId(). An
entity can expose its id as a public property instead (e.g. a
`public private(set) ?int $id` with PHP 8.4 asymmetric visibility, or a
property hook) and have no getId() method at all, so it was displayed with
an object hash that changes on every request (e.g. in the headers of the
CollectionField entries).

The id is now read from getId() or from a public and initialized $id
property. The object hash is still used when there's no id, and also when
the id can't be converted to a string (null, an array or a non-Stringable
object), instead of displaying "<class> #" or failing with "Object of
class X could not be converted to string".
@javiereguiluz

Copy link
Copy Markdown
Collaborator

Thanks @phcorp.

@javiereguiluz
javiereguiluz force-pushed the fix_as_string_id_property branch from 5bc6c64 to cf36fee Compare November 8, 2026 17:56
@javiereguiluz
javiereguiluz merged commit 5fd9516 into EasyCorp:5.x Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants