Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions libraries/src/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1954,4 +1954,27 @@ public function hasField($key)

return property_exists($this, $key);
}

/**
* Returns the data of the current table record as an associative array.
*
* Each element in the array uses the column name as the key and the
* corresponding value from the record as the value. Fields that are not
* set will be returned as null.
*
* @return array The current record data.
*
* @since __DEPLOY_VERSION__
*/
public function getData(): array
{
$data = [];

foreach ($this->getFields() as $field) {
$fieldName = $field->Field;
$data[$fieldName] = $this->{$fieldName} ?? null;
}

return $data;
}
Comment on lines +1969 to +1979
Copy link
Member

Choose a reason for hiding this comment

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

why is this needed? ArrayHelper::fromObject should do the same? or do I miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think if the client code need data from the Table object, the Table class should provide a method to offer that data. We should not have to rely on a helper method from an outside to get necessary data. Having a method like this would be more developer friendly than the other workarounds

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel it is strange that we do not offer this method inside the Table class to expose the data to client code and leave each developer do it in his own way:

  • get_object_vars
  • ArrayHelper::fromObject
  • CMSHelper getRowData
  • CMSHelper getDataObject

Just name a few. Also, please note that the method returns only the actual database columns, in a stable and predictable way while the other methods such as get_object_vars or ArrayHelper::fromObject can return other public properties added to the class. The class itself is the best to provide the data, not from an outsider like this one https://github.com/joomla/joomla-cms/blob/6.1-dev/libraries/src/Helper/CMSHelper.php#L101

}