Important

You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.

See our Release Process documentation for more information on the currently supported and upcoming releases.

Datasources

OroPlatform gives you a wide variety of ways to prepare and supply data to a datagrid by encapsulating all data access in a datasource object. Datagrids can be configured to retrieve data from a PHP array, Doctrine ORM, a search engine or any other source by using datasources that implement DatasourceInterface.

Supported Types

Custom Types

To implement your own datasource type:

  • Create a class that implements DatasourceInterface

  • Register your type as a tagged service - { name: oro_datagrid.datasource, type: YOUR_CUSTOM_TYPE_NAME }

 1 <?php
 2
 3 namespace Acme\Bundle\AcmeBundle\Datagrid\Datasource;
 4
 5 use Oro\Bundle\DataGridBundle\Datagrid\DatagridInterface;
 6 use Oro\Bundle\DataGridBundle\Datasource\DatasourceInterface;
 7 use Oro\Bundle\DataGridBundle\Datasource\ResultRecord;
 8
 9 class CustomDatasource implements DatasourceInterface
10 {
11     const TYPE = 'acme_custom';
12
13     /**
14      * @var array
15      */
16     protected $arraySource = [];
17
18     /** {@inheritdoc} */
19     public function process(DatagridInterface $grid, array $config)
20     {
21         $grid->setDatasource(clone $this);
22     }
23
24     /** {@inheritdoc} */
25     public function getResults()
26     {
27         $rows = [];
28         foreach ($this->arraySource as $result) {
29             $rows[] = new ResultRecord($result);
30         }
31
32         return $rows;
33     }
34
35     /**
36      * @return array
37      */
38     public function getArraySource()
39     {
40         return $this->arraySource;
41     }
42
43     /**
44      * @param array $source
45      */
46     public function setArraySource(array $source)
47     {
48         $this->arraySource = $source;
49     }
50 }

Add the service definition to services.yml:

1 acme_bundle.datagrid.datasource.array:
2     class: Acme\Bundle\AcmeBundle\Datagrid\Datasource\CustomDatasource
3     tags:
4         - { name: oro_datagrid.datasource, type: acme_custom }

Now that you have created your custom datasource type, you can use it in any datagrid. In the configuration of the datagrid tells OroPlatform to use this datasource via the type parameter under the source node:

1 datagrids:
2     DATAGRID_NAME_HERE:
3         source:
4             type: acme_custom

ACL

You can protect a datasource with ACL by adding the acl_resource parameter under the source node in the datagrid configuration:

1 datagrids:
2     DATAGRID_NAME_HERE:
3         source:
4             acl_resource: SOME_ACL_IF_NEEDED