Fusio

Recipes / Sql / Select single row

Routes
resources/routes.yaml
"/test/:id":
  version: 1
  methods:
    GET:
      public: true
      action: "${dir.src}/sql-select-row.php"
Connection
resources/connections.yaml
Database-Connection:
  class: Fusio\Adapter\Sql\Connection\Sql
  config:
    type: "pdo_mysql"
    host: "127.0.0.1"
    username: "app"
    password: "secret"
    database: "app"
Action
resources/sql-select-row.php
<?php
/**
 * @var \Fusio\Engine\ConnectorInterface $connector
 * @var \Fusio\Engine\ContextInterface $context
 * @var \Fusio\Engine\RequestInterface $request
 * @var \Fusio\Engine\Response\FactoryInterface $response
 * @var \Fusio\Engine\ProcessorInterface $processor
 * @var \Psr\Log\LoggerInterface $logger
 * @var \Psr\SimpleCache\CacheInterface $cache
 */

use PSX\Http\Exception as StatusCode;

/** @var \Doctrine\DBAL\Connection $connection */
$connection = $connector->getConnection('Database-Connection');

$todo = $connection->fetchAssoc('SELECT * FROM app_todo WHERE id = :id', [
    'id' => $request->getUriFragment('id')
]);

if (empty($todo)) {
    throw new StatusCode\NotFoundException('Entry not available');
}

return $response->build(200, [], $todo);