Skip to content

Refactor db logic for Table::class and DB::class #20

@arshavinel

Description

@arshavinel

Description

Table::class and DB::class has a bad design. They have a bunch of static methods for interacting with database.


It should be splitted respecting the following:

  • Table::class renamed in TableEntity::class

    • it should have properties for every column;
    • the child should still be able to define files;
      • but it should use a new WithFiles trait for that.
    • it should not be available anymore to interact with the database;
      • because it would represent only a blueprint of a Table.
  • DB::class splitted in DbManager::class and DbConn::class

    • they both are objects (no static methods anymore);

    • DbManager::class is stored by StaticHandler::class

      • its constructor gets database config and generate the DbConn::class objects
        • so it becomes a handler for all connections
    • DbConn::class is stored by DbManager::class

      • it gets the TableEntity::class subclass namespace and the query parameters
      • it returns the desired format (array, json, Entity object)
    • Probably we will create also an DbQuery::class

      • this one would get the query params
      • it should be executed manually with another method

Note: Small inspiration from Symfony 6.

Example

For framework usage:

use Arshwell\Monolith\StaticHandler;
use Arshwell\Monolith\Db\DbManager;
use Arshwell\Monolith\Db\DbConn;
use Arshwell\Monolith\Db\TableEntity;

StaticHandler::setDbManager(new DbManager(
    // database config
));

The new Entity class:

use Arshwell\Monolith\Db\TableEntity;

class User extens TableEntity
{
    // const db table info

    // const files

    // properties for every table column

    // getters and setters
}

For user usage:

use Arshwell\Monolith\StaticHandler;
use Arshwell\Monolith\Db\DbConn;
use MyNamespace\TableEntity\User;

/** @var DbConn $dbConn **/
$dbConn = StaticHandler::getDbManager("main");

$dbConn->query(...)->result(User::class);
$dbConn->query(...)->result(User::class, TableEntity::RESULT_ENTITY);

$dbConn->query(...)->result(User::class, TableEntity::RESULT_ARRAY);
$dbConn->query(...)->result(User::class, TableEntity::RESULT_JSON);

Arshwell Version

0.*

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions