****** WARNING: THIS PROJECT IS NO LONGER MAINTAINED!! ******
Netdudes\DataSourceryBundle
, (*1)
, (*2)
DataSourceryBundle is a neat tool to handle building and performing complex queries on data sets, including support for natural-language queries and safe handling of user provided query parameters., (*3)
Usage (work in progress!)
Assume we have one entity in our system managed by Doctrine, called User
, that looks like this:, (*4)
User {
string username
string nameFirst
string nameLast
\DateTime registered
User bestFriend => OtM with another user
User worstEnemy => OtM with another user
}
You can get the building block of the library, the DataSource
, from a builder. From here and now on we will assume you have a DI container (e.g. Symfony) where the needed services are registered., (*5)
$dataSourceBuilder = $container
->get('netdudes_data_sourcery.data_source.factory')
->createBuilder('My\Entities\User');
With a builder is easy to create a Datasource
, (*6)
$dataSourceBuilder
->addField('username', 'string', 'username')
->addField('bestFriendUsername' 'string', 'bestFriend.username')
->addField('worstEnemyUsername', 'string', 'worstEnemy.username')
->addField('friendOfMyEnemyUsername', 'string', 'worstEnemy.bestFriend.username')
->addField('registered', 'date', 'registered');
$dataSource = $dataSourceBuilder->build();
Alternatively, a data source can be generated from a configuration class, very similarly to how Symfony Forms are built., (*7)
class MyNiceDataSourceConfig implements DataSourceConfigurationInterface
{
public function getEntityClass()
{
return 'My\Entities\User';
}
public function buildDataSource(DataSourceBuilderInterface $builder)
{
$builder
->addField('username', 'string', 'username')
->addField('bestFriendUsername', 'string', 'bestFriend.username')
->addField('worstEnemyUsername', 'string', 'worstEnemy.username')
->addField('friendOfMyEnemyUsername', 'string', 'worstEnemy.bestFriend.username')
->addField('registered', 'date', 'registered');
}
}
$dataSource = $container
->get('netdudes_data_sourcery.data_source.factory')
->createFromConfiguration(new MyNiceDataSourceConfig());
In order to query you data source, you must have a Query
object. Creating one manually is easy:, (*8)
$query = new Query();
$query->setSelect(['username', 'bestFriendUsername', 'worstEnemyUsername', 'friendOfMyEnemyUsername', 'registered']);
$filter = new Filter(
[
new FilterCondition('username', FilterCondition::METHOD_STRING_EQ, 'admin')
]
);
$query->setFilter($filter);
Alternatively you can use the built in parser for the system's language, UQL:, (*9)
$uqlInterpreter = $container->get('netdudes_data_sourcery.uql.interpreter.factory')->create($dataSource);
$filter = $uqlInterpreter->generateFilters('username != "admin"');
$query->setFilter($filter);
Finally, you can get your data from the data source, (*10)
$data = $dataSource->getData($query);
dump($data);
Giving, (*11)
array:1 [
0 => array:5 [
"username" => "admin"
"bestFriendUsername" => "Max"
"worstEnemyUsername" => "John"
"friendOfMyEnemyUsername" => "Max"
"registered" => DateTime {#124
+"date": "2014-02-01 00:00:00.000000"
+"timezone_type": 3
+"timezone": "Europe/Berlin"
}
]
]