Closed MAGlCUS closed 5 years ago
The function with the "anormal" function is getIdentifierValues($entity) in sonata-project/doctrine-orm-admin-bundle/Model/ModelManager.php
public function getIdentifierValues($entity)
{
// Fix code has an impact on performance, so disable it ...
//$entityManager = $this->getEntityManager($entity);
//if (!$entityManager->getUnitOfWork()->isInIdentityMap($entity)) {
// throw new \RuntimeException('Entities passed to the choice field must be managed');
//}
$class = ClassUtils::getClass($entity);
$metadata = $this->getMetadata($class);
$platform = $this->getEntityManager($class)->getConnection()->getDatabasePlatform();
$identifiers = [];
foreach ($metadata->getIdentifierValues($entity) as $name => $value) {
if (!\is_object($value)) {
$identifiers[] = $value;
continue;
}
if (method_exists($value, '__toString')) {
$identifiers[] = (string) $value;
continue;
}
$fieldType = $metadata->getTypeOfField($name);
$type = $fieldType && Type::hasType($fieldType) ? Type::getType($fieldType) : null;
if ($type) {
$identifiers[] = $type->convertToDatabaseValue($value, $platform);
continue;
}
$metadata = $this->getMetadata(ClassUtils::getClass($value));
foreach ($metadata->getIdentifierValues($value) as $value) {
$identifiers[] = $value;
}
}
return $identifiers;
}
if we modified the class adding:
foreach ($metadata->getIdentifierValues($entity) as $name => $value) {
if (!\is_object($value)) {
$identifiers[] = $value;
continue;
}
if (method_exists($value, 'getId')) {
$identifiers[] = (string) $value->getId();
continue;
}
if (method_exists($value, '__toString')) {
$identifiers[] = (string) $value;
continue;
}
All works fine.
I find a issue in SonataDoctrineORMAdminBundle (#923). Perhaps it's better follow that issue.
Can you try to write a PR with a bugfix @MAGlCUS ?
Can you try to write a PR with a bugfix @MAGlCUS ?
@core23, Yes, but it must be in SonataDoctrineORMAdminBundle project ;-)
Can you try to write a PR with a bugfix @MAGlCUS ?
@core23, here you have the PR.
Closed in favor of sonata-project/SonataDoctrineORMAdminBundle#928.
Environment
Sonata packages
Symfony packages
PHP version
Subject
The documentation says that:
But if we have this use-case:
Then you make a sonata admin with this configuration in service.yaml:
If you access to list Action, all the routes are generated with __toString() function of classA and classB. This is correct but not desirable behavior. It is correct if we have a use case like the one in the note (UUID Object to manage the ID), but it is incorrect if we have objects that are not exclusively used to maintain the classC ID.
In the use case exposed, sonata admin must generate URL with classA and classB getId.
When sonata admin use "admin.id(entity)" check if child id entities has __toString() function exposed, but first must verify if getId() is exposed.
In the worst case, sonata admin should check if the entity (classC) has its own getId, and thus could be more versatile in most cases (slugs, id, __toString, etc.).
Steps to reproduce
Create the classes and the admin of the previous paragraph and look at the URL in delete button or edit button.
Expected results
When sonata admin use "admin.id(entity)" check if child id entities has __toString() function exposed, but first must verify if getId() is exposed and generate URL with those values.
Actual results
The URLs are generated with __toString function of child classes.