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
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;
},
I had it working, until it was broken by recent db updates
use Psr\Container\ContainerInterface;
use Yiisoft\ActiveRecord\ActiveRecord;
use Yiisoft\Db\Connection\Connection;
final class DbProvider extends \Yiisoft\Di\Support\ServiceProvider
{
public function register(ContainerInterface $container): void
{
$container->get(Connection::class);
ActiveRecord::connectionId('mysql');
}
}
And BC is to be expected in active development of pre-alpha software, so no need to excuse yourself for that. I assume the changes are all for the better anyway