-
-
Save Tocacar/4337861 to your computer and use it in GitHub Desktop.
// 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); | |
} |
For anyone else who lands here in the future, here is one way to do it :)
In the Admin class (this currently doesn't paginate properly, instead returning all results, hopefully will update here shortly when I figure that out):
public function getDataSourceIterator()
{
$datagrid = $this->getDatagrid();
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->select('DISTINCT ' . $query->getRootAlias());
//$query->setFirstResult($firstResult);
//$query->setMaxResults($maxResult);
return new CustomQuerySourceIterator($query->getQuery(), $this->getExportFields());
}
The CustomQuerySourceIterator:
use Exporter\Source\DoctrineORMQuerySourceIterator;
class CustomQuerySourceIterator extends DoctrineORMQuerySourceIterator
{
protected function getValue($value)
{
//if value is array or collection, creates string
if (is_array($value) or $value instanceof \Traversable) {
$result = array();
foreach ($value as $item) {
$result[] = $this->getValue($item);
}
$value = implode(', ', $result);
//formated datetime output
} elseif ($value instanceof \DateTime) {
$value = $value->format('r');
} elseif (is_object($value)) {
$value = (string) $value;
}
return $value;
}
}
Greetings. how could I do a search of this type in this method getDataSourceIterator.
$query-> select () -> where ('state.name') -> notEqual ('approved');
You can guide me
public function getDataSourceIterator()
{
$datagrid = $this->getDatagrid();
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->select()->where('state.name')->notEqual('approved');
if ($query instanceof ProxyQueryInterface) {
$query->addOrderBy($query->getSortBy(), $query->getSortOrder());
$query = $query->getQuery();
}
return $this->getModelManager()->getDataSourceIterator($this->getDatagrid(), $this->getExportFields());
}
Solvent that way by doing the query before instantiating the buildPager () and it worked correctly.
public function getDataSourceIterator()
{
$datagrid = $this->getDatagrid();
$datagrid->getQuery()->field('state')->equals(APPROVED);
$datagrid->buildPager();
$datasource = $this->getModelManager()->getDataSourceIterator($datagrid, $this->getExportFields());
return $datasource;
}
how i can save the results of this DoctrineORMQuerySourceIterator in my server or send as a attachement in a email ?
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.
Hi Tocacar,
I know this is old. Did you ever find a solution?