amphp/postgres

Usage example with Model classes

madflow opened this issue · 3 comments

Hi there!

In a non-amp context I am used to have Model classes for the database access. I can get the examples to work - but I have a hard time mapping the "amp" way to a model class approach.

So simple something like this:

$dsn = 'postgres://amp:amp@localhost:1234/amp';

Amp\Loop::run(function () use ($dsn) {
    $pool = Postgres\pool($dsn);
    $model = new SomeModel($pool);
    var_dump($model->fetchAll());
   // Array of values
});
class SomeModel
{
    protected $pool;

    public function __construct(Pool $pool)
    {
        $this->pool = $pool;
    }
    
    public function fetchAll()
    {
    }
}

Is there a another prefered way of doing this in an amp context?

Is there maybe an example project where this library is used in a "real world" application?

Thanks!

I guess what you're looking for is a library that automatically maps the data to objects? I guess there's no such library on top of Amp, yet.

There's an (abandoned) project we used to work on that's still based on Amp v1. However, it might give you some hints. Instead of returning arrays there, you could just return objects. See here and here. resolve + private method there would likely be written as Amp\call + inline closure in Amp v2.

Hope this helps!

This actually does help, expecially with the examples in the old project! I used the Amp\call + inline suggestion and made some good progress.

Closing...

You're welcome. :-)