Table of contents

There are these supported datasources so far:

  • Doctrine (QueryBuilder)
  • Doctrine (Collection)
  • Nextras (Collection)
  • Dibi (DibiFluent)
  • Dibi (DibiFluent) for MS-SQL
  • Nette\Database (Please see it's documentation here (opens new window))
  • Nette\Database\Table
  • Nette\Database\Table (for MS-SQL)
  • Nette\Database\Table (for PostgreSQL)
  • Array
  • Elasticsearch
  • Remote Api
  • Any other class that implements IDataSource

You can set data source like this:

$grid->setDataSource($this->ndb->table('user')); // NDBT
$grid->setDataSource($this->dibi->select('*')->from('user')); // Dibi
$grid->setDataSource([['id' => 1, 'name' => 'John'], ['id' => 2, 'name' => 'Joe']]); // Array
$grid->setDataSource($exampleRepository->createQueryBuilder('er')); // Doctrine query builder
# ...
1
2
3
4
5

The primary key column is by default id. You can change that:

$grid->setPrimaryKey('email');
1

Once you have set a data source, you can add columns to the datagrid.

# ORM Relations

When you are using for example Doctrine as a data source, you can easily access another related entities for rendering in column. Let's say you have an entity User and each instance can have a property $name and $grandma. $grandma is also an instance of User class. Displaying people and their grandmas is very simple then - just use this dot notation:

$grid->addColumnText('name', 'Name', 'name');
$grid->addColumnText('grandma_name', 'Grandma', 'grandma.name');
1
2

# ApiDataSource

There is also datasource, that takes data from remote api. It is experimental, you can extend it and overwrite whatever you want.

Basic usage:

$grid->setDataSource(
	new Contributte\Datagrid\DataSource\ApiDataSource('http://my.remote.api')
);
1
2
3

The idea is simply to forward filtering/sorting/limit/... to remote api. Feel free to leave me a comment if you want to add/improve something.

# NextrasDataSource

There is one specific behaviour when using Nextras ORM. When custom filter conditions are used, user has to work not with given Collection instance, but with Collection::getQueryBuilder(). That snippet of code will not work correctly, because DbalCollection calls clone on each of it's methods:

$grid->getFilter('name')
	->setCondition(function ($collection, $value) {
		$collection->limitBy(1);
	});
1
2
3
4

User should use collection's QueryBuilder instead:

$grid->getFilter('name')
	->setCondition(function ($collection, $value) {
		$collection->getQueryBuilder()->andWhere('name LIKE %s', "%$value%");
	});
1
2
3
4

# NetteDatabaseTableDataSource

There is a special feature for NetteDatabaseTableDataSource and referenced/related columns. When you want to reach related column from another table, you can do that using this syntax:

$grid->addColumnText('name', 'Name', ':related_table.name');
1

For referenced table column, just remove the colon:

$grid->addColumnText('name', 'Name', 'referenced_table.name');
1

In case you want to specify the "through-column", use following syntax:

$grid->addColumnText('name', 'Name', ':related_table.name:through_column_id');
$grid->addColumnText('name', 'Name', 'referenced_table.name:through_column_id');
1
2

# ElasticDataSource

$grid->setDataSource(
    new ElasticsearchDataSource(
        $client, // Elasticsearch\Client
        'users', // Index name
        'user' // Index type
    )
);
1
2
3
4
5
6
7