How to use yiisoft/db

I’m trying to use active-record with db-mysql, but I can’t seem to get the connection part right. I have done the following:

Added to common.php

    Connection::class => static function (ContainerInterface $container) use (&$params) {
        $dsn = new Dsn('mysql', '127.0.0.1', $params['secrets']['mysql']['db'], '3306');
        $db = new Connection(
            $container->get(CacheInterface::class),
            $container->get(LoggerInterface::class),
            $container->get(Profiler::class),
            $dsn->getDsn()
        );

        $db->setUsername($params['secrets']['mysql']['user']);
        $db->setPassword($params['secrets']['mysql']['pass']);

        ConnectionPool::setConnectionsPool('mysql', $db);

        return $db;
    },

In some widget:
$items = \App\Model\FeedData::find()->all();

Error:
Argument 1 passed to Yiisoft\Db\Connection\ConnectionPool::getConnectionPool() must be of the type string, null given, called in /yii3/vendor/yiisoft/active-record/src/BaseActiveRecordTrait.php on line 315

Who can point me in the right direction?

final class FeedData extends ActiveRecord
{
    public function __construct()
    {
        static::connectionId('mysql');
    }
}

I don’t think __construct will ever run, as ::find() is called statically.

I tried your code anyway, as I am wrong on a regular base :grimacing: The error did not change.

FeedData::connectionId('mysql');
$items = \App\Model\FeedData::find()->all();

I really appreciate your efforts. Unfortunately it is still no go. But the error message changed!

TypeError: Return value of Yiisoft\Db\Connection\ConnectionPool::getConnectionPool() must be an instance of Yiisoft\Db\Connection\Connection, null returned in /yii3/vendor/yiisoft/db/src/Connection/ConnectionPool.php:13
Stack trace:
#0 /yii3/vendor/yiisoft/active-record/src/BaseActiveRecordTrait.php(315): Yiisoft\Db\Connection\ConnectionPool::getConnectionPool()
#1 /yii3/vendor/yiisoft/active-record/src/ActiveQuery.php(85): Yiisoft\ActiveRecord\BaseActiveRecord::getConnection()
#2 /yii3/vendor/yiisoft/active-record/src/ActiveRecord.php(452): Yiisoft\ActiveRecord\ActiveQuery->__construct()
#3 /yii3/src/Widget/RssFeed.php(29): Yiisoft\ActiveRecord\ActiveRecord::find()
#4 /yii3/vendor/yiisoft/widget/src/Widget.php(106): App\Widget\RssFeed->run()
#5 /yii3/vendor/yiisoft/widget/src/Widget.php(173): Yiisoft\Widget\Widget->render()
#6 /yii3/vendor/yiisoft/factory/src/Definitions/ArrayBuilder.php(163): Yiisoft\Widget\Widget->__toString()
#7 /yii3/vendor/yiisoft/factory/src/Definitions/ArrayBuilder.php(36): Yiisoft\Factory\Definitions\ArrayBuilder->configure()
#8 /yii3/vendor/yiisoft/factory/src/Definitions/ArrayDefinition.php(71): Yiisoft\Factory\Definitions\ArrayBuilder->build()
#9 /yii3/vendor/yiisoft/factory/src/Factory.php(45): Yiisoft\Factory\Definitions\ArrayDefinition->resolve()
#10 /yii3/vendor/yiisoft/widget/src/WidgetFactory.php(38): Yiisoft\Factory\Factory->create()
#11 /yii3/vendor/yiisoft/widget/src/Widget.php(92): Yiisoft\Widget\WidgetFactory::createWidget()
#12 /yii3/mister42/layout/_sidebar2.php(26): Yiisoft\Widget\Widget::widget()
#13 /yii3/vendor/yiisoft/view/src/PhpTemplateRenderer.php(13): require('/yii3/...')
#14 /yii3/vendor/yiisoft/view/src/PhpTemplateRenderer.php(20): Yiisoft\View\PhpTemplateRenderer->Yiisoft\View\{closure}()
#15 /yii3/vendor/yiisoft/view/src/View.php(341): Yiisoft\View\PhpTemplateRenderer->render()
#16 /yii3/vendor/yiisoft/view/src/View.php(244): Yiisoft\View\View->renderFile()
#17 /yii3/mister42/layout/columns.php(66): Yiisoft\View\View->render()
#18 /yii3/vendor/yiisoft/view/src/PhpTemplateRenderer.php(13): require('/yii3/...')
#19 /yii3/vendor/yiisoft/view/src/PhpTemplateRenderer.php(20): Yiisoft\View\PhpTemplateRenderer->Yiisoft\View\{closure}()
#20 /yii3/vendor/yiisoft/view/src/View.php(341): Yiisoft\View\PhpTemplateRenderer->render()
#21 /yii3/src/Controller.php(73): Yiisoft\View\View->renderFile()
#22 /yii3/src/Controller.php(42): App\Controller->renderProxy()
#23 /yii3/vendor/yiisoft/yii-web/src/Data/DataResponse.php(161): App\Controller->App\{closure}()
#24 /yii3/vendor/yiisoft/yii-web/src/Data/Formatter/HtmlDataResponseFormatter.php(25): Yiisoft\Yii\Web\Data\DataResponse->getData()
#25 /yii3/vendor/yiisoft/yii-web/src/Data/DataResponse.php(42): Yiisoft\Yii\Web\Data\Formatter\HtmlDataResponseFormatter->format()
#26 /yii3/vendor/yiisoft/yii-web/src/SapiEmitter.php(101): Yiisoft\Yii\Web\Data\DataResponse->getBody()
#27 /yii3/vendor/yiisoft/yii-web/src/SapiEmitter.php(40): Yiisoft\Yii\Web\SapiEmitter->shouldOutputBody()
#28 /yii3/webroot/index.php(33): Yiisoft\Yii\Web\SapiEmitter->emit()
#29 {main}

What I find very odd is that the code below incommon.php will not exit inside the Connection::class, meaning the code never runs. At least that would explain why there is no connection available…

    Connection::class => static function (ContainerInterface $container) use ($params) {
        exit("Database config loading");
        $dsn = new Dsn('mysql', $params['secrets']['mysql']['host'], $params['secrets']['mysql']['db'], '3306');
        $db = new Connection(
            $container->get(CacheInterface::class),
            $container->get(LoggerInterface::class),
            $container->get(Profiler::class),
            $dsn->getDsn()
        );
        $db->setUsername($params['secrets']['mysql']['user']);
        $db->setPassword($params['secrets']['mysql']['pass']);

        ConnectionPool::setConnectionsPool('mysql', $db);

        return $db;
    },

Any ideas?

Are you running composer dumpautoload after updating common.php?

Yes I did, still no joy :frowning:

shared repo for review code.

I don’t have a repo for this one yet, but I packed it in a small download (49kb).

You have reported the same mistake in this case

Sorry, I have no idea what you tried to say there.

I encountered the same mistake as you, so you can look at the demo case first

If we’re talking the same demo, that uses cycle instead.