Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
Upgrade cakephp/database and cakephp/validation from v4 to v5
  • Loading branch information
odan committed Sep 15, 2023
1 parent d84829d commit 75e01eb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 61 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"ext-json": "*",
"ext-pdo": "*",
"cakephp/chronos": "^3",
"cakephp/database": "^4",
"cakephp/validation": "^4.4",
"cakephp/database": "^5",
"cakephp/validation": "^5",
"fig/http-message-util": "^1.1",
"monolog/monolog": "^3",
"nyholm/psr7": "^1.4",
Expand All @@ -31,7 +31,7 @@
"mikey179/vfsstream": "^1.6",
"phpstan/phpstan": "1.*",
"phpunit/phpunit": "^10",
"selective/test-traits": "^3",
"selective/test-traits": "^4",
"squizlabs/php_codesniffer": "^3",
"symfony/console": "^6",
"symfony/dotenv": "^6",
Expand Down
10 changes: 6 additions & 4 deletions config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@
},

PDO::class => function (ContainerInterface $container) {
$db = $container->get(Connection::class);
$driver = $db->getDriver();
$driver->connect();
$driver = $container->get(Connection::class)->getDriver();

return $driver->getConnection();
$class = new ReflectionClass($driver);
$method = $class->getMethod('getPdo');
$method->setAccessible(true);

return $method->invoke($driver);
},

ErrorMiddleware::class => function (ContainerInterface $container) {
Expand Down
69 changes: 15 additions & 54 deletions src/Factory/QueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,45 @@
namespace App\Factory;

use Cake\Database\Connection;
use Cake\Database\Query;
use Cake\Database\Query\DeleteQuery;
use Cake\Database\Query\InsertQuery;
use Cake\Database\Query\SelectQuery;
use Cake\Database\Query\UpdateQuery;

/**
* Factory.
*/
final class QueryFactory
{
private Connection $connection;

/**
* The constructor.
*
* @param Connection $connection The database connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* Create a new 'select' query for the given table.
* @param string $table
*
* @param string $table The table name
*
* @return Query A new select query
* @return SelectQuery<mixed>
*/
public function newSelect(string $table): Query
public function newSelect(string $table): SelectQuery
{
return $this->newQuery()->from($table);
return $this->connection->selectQuery([], $table);
}

/**
* Create a new query.
*
* @return Query The query
*/
public function newQuery(): Query
{
return $this->connection->newQuery();
}

/**
* Create an 'update' statement for the given table.
*
* @param string $table The table to update rows from
* @param array $data The values to be updated
*
* @return Query The new update query
*/
public function newUpdate(string $table, array $data): Query
public function newUpdate(string $table, array $data = []): UpdateQuery
{
return $this->newQuery()->update($table)->set($data);
return $this->connection->updateQuery($table)->set($data);
}

/**
* Create an 'update' statement for the given table.
*
* @param string $table The table to update rows from
* @param array $data The values to be updated
*
* @return Query The new insert query
*/
public function newInsert(string $table, array $data): Query
public function newInsert(string $table, array $data): InsertQuery
{
return $this->newQuery()->insert(array_keys($data))
return $this->connection->insertQuery($table)
->insert(array_keys($data))
->into($table)
->values($data);
}

/**
* Create a 'delete' query for the given table.
*
* @param string $table The table to delete from
*
* @return Query A new delete query
*/
public function newDelete(string $table): Query
public function newDelete(string $table): DeleteQuery
{
return $this->newQuery()->delete($table);
return $this->connection->deleteQuery($table);
}
}

0 comments on commit 75e01eb

Please sign in to comment.