Skip to content

Commit

Permalink
Deprecated FieldDescription::getTargetEntity()
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed May 24, 2020
1 parent 472ab29 commit 3ebeb5b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/Admin/FieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,28 @@ public function setAssociationMapping($associationMapping)
$this->fieldName = $associationMapping['fieldName'];
}

/**
* NEXT_MAJOR: Remove this method.
*/
public function getTargetEntity()
{
@trigger_error(sprintf(
'Method %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x and will be removed in version 4.0.'
.' Use %s::getTargetModel() instead.',
__METHOD__,
__CLASS__
), E_USER_DEPRECATED);

return $this->getTargetModel();
}

public function getTargetModel(): ?string
{
if ($this->associationMapping) {
return $this->associationMapping['targetEntity'];
}

return null;
}

public function setFieldMapping($fieldMapping)
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/DatagridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionIn

if (ModelAutocompleteFilter::class === $type) {
$fieldDescription->mergeOption('field_options', [
'class' => $fieldDescription->getTargetEntity(),
'class' => $fieldDescription->getTargetModel(),
'model_manager' => $fieldDescription->getAdmin()->getModelManager(),
'admin_code' => $admin->getCode(),
'context' => 'filter',
Expand Down
8 changes: 4 additions & 4 deletions src/Builder/FormContractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescrip
));
}

$options['class'] = $fieldDescription->getTargetEntity();
$options['class'] = $fieldDescription->getTargetModel();
$options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();

if ($this->checkFormClass($type, [ModelAutocompleteType::class])) {
Expand All @@ -123,7 +123,7 @@ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescrip
'The current field `%s` is not linked to an admin.'
.' Please create one for the target entity: `%s`',
$fieldDescription->getName(),
$fieldDescription->getTargetEntity()
$fieldDescription->getTargetModel()
));
}
}
Expand All @@ -133,7 +133,7 @@ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescrip
'The current field `%s` is not linked to an admin.'
.' Please create one for the target entity : `%s`',
$fieldDescription->getName(),
$fieldDescription->getTargetEntity()
$fieldDescription->getTargetModel()
));
}

Expand Down Expand Up @@ -165,7 +165,7 @@ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescrip
'The current field `%s` is not linked to an admin.'
.' Please create one for the target entity : `%s`',
$fieldDescription->getName(),
$fieldDescription->getTargetEntity()
$fieldDescription->getTargetModel()
));
}

Expand Down
12 changes: 6 additions & 6 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,12 @@ public function getNormalizedIdentifier($entity)
UnitOfWork::STATE_NEW,
UnitOfWork::STATE_REMOVED,
], true)) {
// NEXT_MAJOR: Uncomment the following exception, remove the deprecation and the return statement inside this conditional block.
// throw new \InvalidArgumentException(sprintf(
// 'Can not get the normalized identifier for %s since it is in state %u.',
// ClassUtils::getClass($entity),
// $this->getEntityManager($entity)->getUnitOfWork()->getEntityState($entity)
// ));
// NEXT_MAJOR: Uncomment the following exception, remove the deprecation and the return statement inside this conditional block.
// throw new \InvalidArgumentException(sprintf(
// 'Can not get the normalized identifier for %s since it is in state %u.',
// ClassUtils::getClass($entity),
// $this->getEntityManager($entity)->getUnitOfWork()->getEntityState($entity)
// ));

@trigger_error(sprintf(
'Passing an object which is in state %u (new) or %u (removed) as argument 1 for %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x'
Expand Down
4 changes: 2 additions & 2 deletions tests/Admin/FieldDescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ public function testGetTargetEntity(): void

$field = new FieldDescription();

$this->assertNull($field->getTargetEntity());
$this->assertNull($field->getTargetModel());

$field->setAssociationMapping($assocationMapping);

$this->assertSame('someValue', $field->getTargetEntity());
$this->assertSame('someValue', $field->getTargetModel());
}

public function testIsIdentifierFromFieldMapping(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Builder/FormContractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testDefaultOptionsForSonataFormTypes(): void

$fieldDescription = $this->createMock(FieldDescriptionInterface::class);
$fieldDescription->method('getAdmin')->willReturn($admin);
$fieldDescription->method('getTargetEntity')->willReturn($modelClass);
$fieldDescription->method('getTargetModel')->willReturn($modelClass);
$fieldDescription->method('getAssociationAdmin')->willReturn($admin);
$admin->method('getNewInstance')->willReturn($model);

Expand Down

0 comments on commit 3ebeb5b

Please sign in to comment.