export to csv only custom fields from object list

3,838 views
Skip to first unread message

Grzegorz Floryański

unread,
Nov 8, 2012, 10:37:45 AM11/8/12
to sonata...@googlegroups.com
Hi,

Is there a simple way to export to csv only list of only one field ( for example an email ) instead of list of whole objects ?

Grzegorz Floryański

unread,
Nov 8, 2012, 11:06:08 AM11/8/12
to sonata...@googlegroups.com
I find a way. Maybe it will help somebody.

We have to overwrite:
    public function exportAction(Request $request)
    {
$what = Array('telephone','email');
$format = $request->get('format');

        $allowedExportFormats = (array) $this->admin->getExportFormats();

        if(!in_array($format, $allowedExportFormats) ) {
            throw new \RuntimeException(sprintf('Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`', $format, $this->admin->getClass(), implode(', ', $allowedExportFormats)));
        }

        $filename = sprintf('export_%s_%s.%s',
            strtolower(substr($this->admin->getClass(), strripos($this->admin->getClass(), '\\') + 1)),
            date('Y_m_d_H_i_s', strtotime('now')),
            $format
        );
        $datagrid = $this->admin->getDatagrid();
        $datagrid->buildPager();

        $flick =  $this->admin->getModelManager()->getDataSourceIterator($datagrid, $what);
    
        return $this->get('sonata.admin.exporter')->getResponse($format, $filename, $flick);

Steve Sobel

unread,
Nov 8, 2012, 11:36:56 AM11/8/12
to sonata...@googlegroups.com
Thank you very much for posting this - I was looking for some help with this a couple of weeks ago and wish I'd seen this!

I'd definitely suggest the Sonata team add this to their documentation on the site!

Steve


--
You received this message because you are subscribed to the Google Groups "sonata-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sonata-users/-/FiyyyziKOP4J.

To post to this group, send email to sonata...@googlegroups.com.
To unsubscribe from this group, send email to sonata-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sonata-users?hl=en.

Andrew Starlike

unread,
Jan 9, 2014, 10:53:04 AM1/9/14
to sonata...@googlegroups.com
thanks a lot for the easy fix... it saved my some time :)

Juan Torres

unread,
Apr 14, 2014, 5:35:23 PM4/14/14
to sonata...@googlegroups.com
Thank you for the answer, it was very helpful to understand better the export tool.

I know this is an old post, and I thought I would complement the answer with an alternative way to select which fields you want to export:

Instead of overwriting the exportAction method, overwrite the getExportFields in the admin class. This method will return an array equivalent to the $what array in the example above.

    public function getExportFields()
    {
        return array('telephone','email');
    }

Now you don't have to create a custom controller to do this :)

kra...@gmail.com

unread,
Dec 21, 2014, 1:10:57 PM12/21/14
to sonata...@googlegroups.com
Maybe it will be helpful to someone - I made a function which exports only items which are visible on the list and not all items from the class. Also order of columns is exactly the same as on list of items.

In addition this works with related entities while original fefault version takes only $metadata->getFieldNames();

    public function getExportFields()
   
{
        $ret
= array();
        $list
= $this->getList();


       
foreach($list->getElements() as $k=>$v){
           
if($k != "_action"){
                $ret
[] = $k;
                $names
[] = $v->getOption('label');
           
}
       
}


       
return $ret;
   
}

Kwame Bentil

unread,
Jan 14, 2015, 11:09:25 AM1/14/15
to sonata...@googlegroups.com
This function didnt work, any clues?

kra...@gmail.com

unread,
Jan 22, 2015, 2:40:19 AM1/22/15
to sonata...@googlegroups.com
Hi!

Can you say what happened ? In what way it didn't work?

Did you get  all fields in export or just had some error?

Where did you put it? Did you try to debug if code is executed?

Please provide at least some details.

Ania

Gwendal

unread,
Feb 9, 2015, 5:40:14 AM2/9/15
to sonata...@googlegroups.com
Hello Ania

I'm not working with Kwane, but I also had a problem using the code you kindly shared with us :
"Neither the property "batch" nor one of the methods "getBatch()", "batch()", "isBatch()", "hasBatch()", "__get()" exist and have public access in class "SO\ProposalBundle\Entity\Sessionproposal"

Do I need something special in the Entity class Sessionproposal I tried to export ?

I've put your code within the Admin class mapped to display this entity. Export is working fine without this code, but only exports fields from the entity, I definitely need to export the related entities values as displayed in the list of items

Your help is welcomed
Thanks,
Gwendal

Ania K

unread,
Feb 9, 2015, 6:18:16 AM2/9/15
to sonata...@googlegroups.com
Ok, I know what is the problem. I had batch option turned off in my project so it was working for me. I didn't have the batch column.

Try to modify it like this:

public function getExportFields()
{
$ret = array();
$list = $this->getList();

    $names = array();

$excluded_columns = array("batch","_action");

foreach($list->getElements() as $k=>$v){

        if(!in_array($k,$excluded_columns)){
$ret[] = $k;
$names[] = $this->trans(/** @Ignore */ $v->getOption('label'));
}
}

return array($ret,$names);
}

You can add more columns to $excluded_columns is necessary.

Ania

Ania K

unread,
Feb 9, 2015, 6:21:22 AM2/9/15
to sonata...@googlegroups.com
One more thing, you can simply use:

$names[] = $v->getOption('label');

instead of :

$names[] = $this->trans($v->getOption('label'));

if you don't need translations. I pasted version from my project and because my website is not in English so I need to translate column names in export as well.

Ania
Reply all
Reply to author
Forward
0 new messages