Fusio

Recipes / Sql / Update row

Routes
resources/routes.yaml
"/test/:id":
  version: 1
  methods:
    PUT:
      public: true
      action: "${dir.src}/sql-update.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-update.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');

$body = $request->getBody();
$now  = new \DateTime();

if (empty($body->title)) {
    throw new StatusCode\BadRequestException('No title provided');
}

$affected = $connection->update('app_todo', [
    'title' => $body->title,
    'lastUpdated' => $now->format('Y-m-d H:i:s'),
], [
    'id' => $request->getUriFragment('id')
]);

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

return $response->build(200, [], [
    'success' => true,
    'message' => 'Update successful',
]);