Difference between #13 and #14 of
Yii3 - How to start

Revision #14 has been created by rackycz rackycz on Oct 8, 2025, 2:30:56 PM with the memo:

edit
« previous (#13) next (#15) »

Changes

Title unchanged

Yii3 - How to start

Category unchanged

Tutorials

Yii version unchanged

3.0

Tags unchanged

Content changed

[...]
It means you have to update dns, user and password in file "config/common/params.php" based on what is written in "docker/dev/compose.yml".

If you run "make yii migrate:up" it should work now and your DB should contain the first table. Check it via adminer:
http://localhost:9081/?server=db&username=db&db=db

## Reading data from DB
 
 
In Yii we were always using ActiveRecord and its models, but in Yii3 the package is not ready yet. The solution is to use existing class `Yiisoft\Db\Query\Query`.
 
 
Open class `src/Api/IndexAction.php` and modify it a little to return all users via your REST API. You have more options:
 
 
You can manually instantiate the Query object, but you need to provide the DB connection manually:
 
 
```php
 
declare(strict_types=1);
 
namespace App\Api;
 
use App\Api\Shared\ResponseFactory;
 
use App\Shared\ApplicationParams;
 
use Psr\Http\Message\ResponseInterface;
 
use Yiisoft\Db\Connection\ConnectionInterface;
 
use Yiisoft\Db\Query\Query;
 
 
final class IndexAction
 
{
 
    public function __invoke(
 
        ResponseFactory     $responseFactory,
 
        ApplicationParams   $applicationParams,
 
        ConnectionInterface $db,
 
    ): ResponseInterface
 
    {
 
        $query = (new Query($db))
 
            ->select('*')
 
            ->from('user');
 
        return $responseFactory->success($query->all());
 
    }
 
}
 
```
 
 
Or you can use the DI container to provide you with the instance. I like this better as I can omit input parameters:
 
 
 
```php
 
declare(strict_types=1);
 
namespace App\Api;
 
use App\Api\Shared\ResponseFactory;
 
use App\Shared\ApplicationParams;
 
use Psr\Container\ContainerInterface;
 
use Psr\Http\Message\ResponseInterface;
 
use Yiisoft\Db\Query\Query;
 
 
final class IndexAction
 
{
 
    public function __invoke(
 
        ResponseFactory    $responseFactory,
 
        ApplicationParams  $applicationParams,
 
        ContainerInterface $container,
 
    ): ResponseInterface
 
    {
 
        $query = $container->get(Query::class)
 
            ->select('*')
 
            ->from('user');
 
        return $responseFactory->success($query->all());
 
    }
 
}
 
```
 
 
Now you can call the URL and see all the users. (If you entered some)
 
http://localhost:9080
1 0
2 followers
Viewed: 592 times
Version: 3.0
Category: Tutorials
Tags:
Written by: rackycz rackycz
Last updated by: rackycz rackycz
Created on: Oct 8, 2025
Last updated: 2 days ago
Update Article

Revisions

View all history