Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document SQL query builder #173

Closed
chriskapp opened this issue Jul 7, 2018 · 1 comment
Closed

Document SQL query builder #173

chriskapp opened this issue Jul 7, 2018 · 1 comment

Comments

@chriskapp
Copy link
Member

Fusio has a quite powerful SQL builder to produce nested results based on SQL queries. This feature is more hidden since we have no real documentation about this, which is a shame since it is a really great feature.

We should add a chapter with examples in our documentation to explain how it is possible to use it since it is a really common use case. See also #168 (comment)

The following shows an example taken from a project, how to build a nested structure based on multiple SQL queries.

$startIndex = (int) $request->getParameter('startIndex');
$startIndex = $startIndex <= 0 ? 0 : $startIndex;
$condition  = getCondition($request);
$builder    = new \PSX\Sql\Builder($connection);

$sql = 'SELECT supplier.id,
               supplier.name,
               supplier.insertDate
          FROM app_supplier supplier
         WHERE supplier.status = 1
           AND ' . $condition->getExpression($connection->getDatabasePlatform()) . '
      ORDER BY supplier.name ASC
         LIMIT :startIndex, 32';

$parameters = array_merge($condition->getValues(), ['startIndex' => $startIndex]);
$definition = [
    'totalResults' => $builder->doValue('SELECT COUNT(*) AS cnt FROM app_supplier WHERE status = 1', [], $builder->fieldInteger('cnt')),
    'startIndex' => $startIndex,
    'entries' => $builder->doCollection($sql, $parameters, [
        'id' => $builder->fieldInteger('id'),
        'name' => 'name',
        'count' => $builder->doValue('SELECT COUNT(*) AS cnt FROM app_article WHERE supplierId = :supplierId', ['supplierId' => new \PSX\Sql\Reference('id')], $builder->fieldInteger('cnt')),
        'insertDate' => $builder->fieldDateTime('insertDate'),
        'links' => [
            'self' => $builder->fieldReplace('/supplier/{id}'),
        ]
    ])
];

return $response->build(200, [], $builder->build($definition));
@chriskapp
Copy link
Member Author

We have now added a chapter to our documentation:
https://fusio.readthedocs.io/en/latest/development/sql_builder.html

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

No branches or pull requests

1 participant