Skip to content

Instantly share code, notes, and snippets.

@Tocacar
Created December 19, 2012 16:06
Show Gist options
  • Save Tocacar/4337861 to your computer and use it in GitHub Desktop.
Save Tocacar/4337861 to your computer and use it in GitHub Desktop.
I need to create a custom DataSourceIterator so that I can pass a related collection's properties to the sonata admin bundle's exportAction (I think this is the only way to achieve the export of one-to-many values, I may be wrong). I have traced backwards how my admin class is currently generating a DataSourceIterator (I think) - see below. Pres…
// vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Admin/Admin.php
/**
* @return
*/
public function getDataSourceIterator()
{
$datagrid = $this->getDatagrid();
$datagrid->buildPager();
return $this->getModelManager()->getDataSourceIterator($datagrid, $this->getExportFields());
}
// $this->getModelManager() is as follows:
/**
* {@inheritdoc}
*/
public function getModelManager()
{
return $this->modelManager;
}
// and $this->modelManager is set using this method:
/**
* @param \Sonata\AdminBundle\Model\ModelManagerInterface $modelManager
*/
public function setModelManager(ModelManagerInterface $modelManager)
{
$this->modelManager = $modelManager;
}
// so, the getDataSourceIterator method that is called once the modelManager property is retrieved is defined in the ModelManagerInterface class and looks like this:
// vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Model/ModelManagerInterface.php
/**
* @param DatagridInterface $datagrid
* @param array $fields
* @param null $firstResult
* @param null $maxResult
*
* @return \Exporter\Source\SourceIteratorInterface
*/
function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null);
// and is implemented, from what I can tell, in the ModelManager class like this:
// vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php
/**
* {@inheritdoc}
*/
public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null)
{
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->select('DISTINCT ' . $query->getRootAlias());
$query->setFirstResult($firstResult);
$query->setMaxResults($maxResult);
return new DoctrineORMQuerySourceIterator($query instanceof ProxyQuery ? $query->getQuery() : $query, $fields);
}
@Saif-hub24
Copy link

I just want to override the query in the getDataSourceIterator function or export query. I want to change entire query with multiple joins so that I can get my required field exported. Is it possible? Any help is much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment