Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AdminListBundle] Display associations when using viewAction() #2564

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -616,19 +616,27 @@ public function getValue($item, $columnName)
public function getStringValue($item, $columnName)
{
$result = $this->getValue($item, $columnName);

if (\is_bool($result)) {
return $result ? 'true' : 'false';
}

if ($result instanceof \DateTimeInterface) {
return $result->format('Y-m-d H:i:s');
}

if ($result instanceof PersistentCollection) {
$results = [];

/* @var Object $entry */
foreach ($result as $entry) {
$results[] = $entry->getName();
$value = $this->getObjectValue($entry);

if (!empty($value)) {
$results[] = $value;
}
}

if (empty($results)) {
return '';
}
Expand All @@ -643,6 +651,31 @@ public function getStringValue($item, $columnName)
return $result;
}

/**
* @param object $object
* @return string|null
*/
private function getObjectValue($object)
{
$methods = [
'getName',
'__toString',
'getTitle',
];

foreach ($methods as $method) {
if (method_exists($object, $method)) {
return $object->$method();
}
}

if ($object instanceof AbstractEntity) {
return (string) $object->getId();
}

return null;
}

/**
* @return string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ protected function doViewAction(AbstractAdminListConfigurator $configurator, $en
throw $this->createAccessDeniedException('You do not have sufficient rights to access this page.');
}

$MetaData = $em->getClassMetadata($configurator->getRepositoryName());
$fields = array();
$metaData = $em->getClassMetadata($configurator->getRepositoryName());
$fields = [];
$accessor = PropertyAccess::createPropertyAccessor();
foreach ($MetaData->fieldNames as $value) {
foreach ($metaData->getReflectionProperties() as $value => $reflectionProperty) {
$fields[$value] = $accessor->getValue($helper, $value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add a check here to see if the object has a __toString method, otherwise skip the property. Something like

if (\is_object($value) && !method_exists($value, '__toString')) {
    continue;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would not return the expected results. The value could be returned by $accessor->getValue($helper, $value); without having __toString() implemented in the entity. Adding your suggestion would cause no values to be displayed.

I've just tested this with an entity that has relations without having __toString() implemented. It caused an error. But the error came from the method getStringValue() from AbstractAdminListConfigurator, because it just tries to access getName() on every entity in the collection (eventhough that most likely does not exist).

I get that you cant add this if it breaks rendering objects which don't have the __toString method. I think at this point it should only break for anyone who has overwritten the getStringValue() method, while not calling parent::getStringValue() and not taking in account the $result could be an instance of PersistentCollection. I don't know what the policy is that you guys have on this, but if this is not good enough then you can close this pull request :)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% for key, value in fields %}
<tr>
<td>
{{ key }}
{{ key | trans }}
</td>
<td>
{{ adminlistconfigurator.getStringValue(entity, key) }}
Expand Down