Service of type Nette\Database\Connection not found
- zrnecx
- Member | 3
Hello, i am using \Nette\DI and \Nette\Database\Connection
i have container like this:
class Container extends \Nette\DI\Container
{
public function __construct(array $params = [])
{
parent::__construct($params);
}
function createServiceDatabase(): \Nette\Database\Connection
{
return new \Nette\Database\Connection(
$this->parameters['dsn'],
$this->parameters['user'],
$this->parameters['password']
);
}
}
My app is parsing url to call method doWorld in this class:
namespace Command;
class Hello extends BaseCommand
{
public function doWorld(\Nette\Database\Connection $db)
{
$db->query("INSERT INTO hello_world",["example_column"=>"hello world"]);
}
}
and i want it call like this:
$Class = new \Command\Hello();
$container->callMethod([$Class,"doWorld"]);
but it throws error
Service of type Nette\Database\Connection needed by $db in Command\Hello::doWorld() not found. Did you register it in configuration file?
I don't know how to fix it, because when i do this:
//This returns Nette\Database\Connection
Debugger::barDump($container->getService("database"));
//This throws "Service of type Nette\Database\Connection not found."
Debugger::barDump($container->getByType("\Nette\Database\Connection"));
Why?
Last edited by zrnecx (2019-09-12 12:06)
- Marek Bartoš
- Nette Blogger | 1263
Is the container generated by compiler or did you extend container and wrote
createServiceDatabase()
method by your own? Because for
getByType()
you need also mapping in $wiring
property
- Marek Bartoš
- Nette Blogger | 1263
Container is meant to be always generated, writing it by your own is waste of
time. But generating is quite complex (see
here)
So I suggest you to use nette/bootstrap and register
services in neon instead.
Here
and here
is the example how to use it.