I have two database tables, Foo and Bar. Both have controllers and models. I want to create an API to export spreadsheets from either table. To this end, I have set up a
CsvView and a
XlsxView. However, the spreadsheet columns needed for Foo are different from Bar (and they are not just the database columns either). To make matters worse, I have some helper methods to serialize e.g. lists in cells.
I thought of converting Foo and Bar entities to table rows in the view (because it is part of creating a representation of the data), but I do not want to create
BarXlsxView because that gets messy quick. I have not found another nice way to do this in the view part. I could perhaps try something with modifying the respective
viewVars in a template file and then feed those to the serializer, but that does not seem right.
Instead, I added a
getTableRow() method to
Entity/Bar.php, and made a
TableExportTrait for the helper methods. Is that a better way of doing things? Alternatively, I could make a helper or view trait with
getBarRow() etc. and use a switch statement, but that sounds a bit extra in comparison.