You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
minor EasyCorp#7113 Refactor EntityDto: deprecate 3 methods and allow access to ClassMeta… (michaelKaefer)
This PR was merged into the 4.x branch.
Discussion
----------
Refactor EntityDto: deprecate 3 methods and allow access to ClassMeta…
Another refactoring. Please just tell me if you have better things to do than checking my refactoring PRs 😬
Notes:
- I paid a lot of attention to not break anything and not change any existing behaviour of the code
- I deprecated the methods because they are just wrappers over `ClassMetadata` and provide no extra value IMHO. If they don't exist anymore there is less EA code to maintain and IMHO the client code is easier to follow if there is no extra wrapper between Doctrine and the client code.
- `getPropertyDataType()` is not only a wrapper over `ClassMetadata` but also mixes `FieldMapping` with `AssociationMapping` and so it returns sometimes `string` sometimes `int` -> so it makes things even more complicated than just using `ClassMetadata` itself. I also plan a PR for deprecating `getPropertyMetadata` (in the first place because it mixes `FieldMapping` with `AssociationMapping`).
- I replaced some `\array_key_exists` with `isset` simply because Doctrine ORM (and also `Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser`) checks the same thing with `isset` instead of `\array_key_exists`
- in 4.0.0 my change introduces 9 times the following code which looks a little ugly:
```php
// Doctrine ORM 2.x returns an array and Doctrine ORM 3.x returns a FieldMapping object
/** `@var` FieldMapping|array $fieldMapping */
/** `@phpstan`-ignore-next-line */
$fieldMapping = $entityDto->getClassMetadata()->getFieldMapping($propertyName);
if (\is_array($fieldMapping)) {
$type = $fieldMapping['type'];
} else {
$type = $fieldMapping->type;
}
```
But in 5.0.0 all occurances can be changed from 9 lines to 1 line:
```php
$type = $entityDto->getClassMetadata()->getFieldMapping($propertyName)->type;
```
Commits
-------
ccb844f Refactor EntityDto: deprecate 3 methods and allow access to ClassMetadata instead
@@ -215,42 +215,52 @@ public function getPropertyMetadata(string $propertyName): KeyValueStore
215
215
thrownew \InvalidArgumentException(sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->getFqcn()));
216
216
}
217
217
218
+
publicfunctiongetClassMetadata(): ClassMetadata
219
+
{
220
+
return$this->metadata;
221
+
}
222
+
223
+
/**
224
+
* @deprecated since 4.27 and to be removed in 5.0, use $entityDto->getClassMetadata()->getFieldMapping($propertyName)->type and $entityDto->getClassMetadata()->getAssociationMapping($propertyName)->type() instead
thrownew \RuntimeException(sprintf('Could not guess a field class for "%s" field. It possibly is an association field or an embedded class field.', $fieldDto->getProperty()));
thrownew \RuntimeException(sprintf('The Doctrine type of the "%s" field is "%s", which is not supported by EasyAdmin. For Doctrine\'s Custom Mapping Types have a look at EasyAdmin\'s field docs.', $fieldDto->getProperty(), $doctrinePropertyType));
180
+
thrownew \RuntimeException(sprintf('The Doctrine type of the "%s" field is "%s", which is not supported by EasyAdmin. For Doctrine\'s Custom Mapping Types have a look at EasyAdmin\'s field docs.', $fieldDto->getProperty(), $doctrineFieldMappingType));
if (true === $field->getCustomOption(AssociationField::OPTION_RENDER_AS_EMBEDDED_FORM)) {
59
-
if (false === $entityDto->isToOneAssociation($propertyName)) {
59
+
if (false === $entityDto->getClassMetadata()->isSingleValuedAssociation($propertyName)) {
60
60
thrownew \RuntimeException(
61
61
sprintf(
62
62
'The "%s" association field of "%s" is a to-many association but it\'s trying to use the "renderAsEmbeddedForm()" option, which is only available for to-one associations. If you want to use a CRUD form to render to-many associations, use a CollectionField instead of the AssociationField.',
@@ -130,11 +130,11 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
130
130
// * the route is not found, which happens when the associated entity is not accessible from this dashboard; do nothing in that case either.
131
131
}
132
132
} else {
133
-
if ($entityDto->isToOneAssociation($propertyName)) {
133
+
if ($entityDto->getClassMetadata()->isSingleValuedAssociation($propertyName)) {
134
134
$this->configureToOneAssociation($field);
135
135
}
136
136
137
-
if ($entityDto->isToManyAssociation($propertyName)) {
137
+
if ($entityDto->getClassMetadata()->isCollectionValuedAssociation($propertyName)) {
0 commit comments