GaryZ
GaryZ

Reputation: 101

SonataAdminBundle Exporter issue with many to many

currently I'm using sonata admin bundle to export a "order" data, how can I export the data with manytomany relationship? I saw a post, it helps a bit, but I still not sure how to get the data in many to many relationship.

Here is my code: public function getExportFields() {

    return [
        $this->getTranslator()->trans('Order Number') => 'id', 
        $this->getTranslator()->trans('First Name') => 'customer.First_name',
        $this->getTranslator()->trans('Last Name') => 'customer.Last_name',
        ...]

Here is fine, but when I try to get 'OrderToProduct' or 'product.name' it failed or only output empty string. I spent to much time on this already, hope someone can give a clue. Thank you.

Upvotes: 0

Views: 1023

Answers (1)

tiriana
tiriana

Reputation: 668

Well, you can't use product.name, because product is a collection. Export action iterates through objects and get properties with path defined and tries to stringify them.

What you need to do is a workaround - define some method in Order class - i.e. getProductsAsString(), make it return string you need, and then add

$this->getTranslator()->trans('Products') => 'productsAsString'

But still - it will put whole string in single cell of xls, csv, xml you are trying to export.

Upvotes: 1

Related Questions