forrest79 / phpgsql

Simple and fast PHP database library for PostgreSQL with auto converting DB types to PHP and fluent interface for SQL query writing.
Other
11 stars 3 forks source link

Row interface ala Rowable #14

Closed h4kuna closed 4 years ago

h4kuna commented 4 years ago

The goal is create Empty object if data is not defined.

Example:

Actual behavior, we must define nullabble return type

Model:

public function loadPageById(int $pageId): ?Database\Row
{
    return $this->table()
        ->where('pw.id', $pageId)
        ->select(['title' => 'pwl.title', 'text' => 'pwl.text'])
        ->fetch();
}

Presenter:

$page = $model->loadPageById($pageTypeId);
$template = $this->getTemplate();
if ($page === NULL) {
    $template->text = NULL;
    $template->title = NULL;
} else {
    $template->text = $page->text;
    $template->title = $page->title;
}

This update allow this: return object everytime

public function loadPageById(int $pageId): Rowable
{
    $row = $this->table()
        ->where('pw.id', $pageId)
        ->select(['title' => 'pwl.title', 'text' => 'pwl.text'])
        ->fetch();
    if ($row === NULL) {
        $row = new DummyRow([
            'title' => NULL,
            'text' => '' // here for example i want empty string
        ]);
    }
    return $row;
}

Presenter:

$page = $model->loadPageById($pageTypeId);
$template = $this->getTemplate();
$template->text = $page->text;
$template->title = $page->title;
forrest79 commented 4 years ago

Idea is good. This could be already done without Rowable interface. DummyRow extends Row and you can returning still Row. In the future, we can update Row, so it could be easily manually created, but it's already possbile now. I'm planning discuss this funcionality internally and we will see then. Thanks.