Problem with Nette\Databases\Context – nette 2.3

5 years ago

lukin
Member | 13
+
-1
-

I use Nette\Databases\Conection for conection o DB and than apply Context. If I use Context I get an exception.

Argument 2 passed to Nette\Database\Context::__construct() must be an instance of Nette\Database\IStructure, null given, called in

On version nette 2.2.7 is OK, in version 2.3 not working.

<?php
use Nette\Database\Connection,
    Nette\Database\Context;
...
$conn_agenda = new Connection('mysql:host='.$DB_AGENDA->HOST.';dbname='.$DB_AGENDA->DB, $DB_AGENDA->USER, $DB_AGENDA->PASSWD);
$db_agenda = new Context($conn_agenda);
?>

5 years ago

greeny
Member | 406
+
0
-

First of all, you should get your services (e.g. Database) by DI.

The error you are getting is pretty much user-friendly. You should read it. Missing second argument for Nette\Database\Context, you have to pass instance of Nette\Database\IStructure there. Which needs connection and cache. Which needs cache storage. This is way to hell, use DI.

5 years ago

Michal Vyšinský
Member | 614
+
+3
-

You need to create instance of Nette\Database\Structure which constructor accepts Connection and CacheStorage.

You can do it like a here:https://github.com/…grations.php#L20

5 years ago

lukin
Member | 13
+
0
-

greeny wrote:

First of all, you should get your services (e.g. Database) by DI.

The error you are getting is pretty much user-friendly. You should read it. Missing second argument for Nette\Database\Context, you have to pass instance of Nette\Database\IStructure there. Which needs connection and cache. Which needs cache storage. This is way to hell, use DI.

Yes, I know.
But it is very old project. The project not use DI.

Problem is, if anyone try out the example of documentation.
https://doc.nette.org/cs/2.4/database

Example from url not working in nette 2.3.

5 years ago

lukin
Member | 13
+
0
-

Michal Vyšinský wrote:

You need to create instance of Nette\Database\Structure which constructor accepts Connection and CacheStorage.

You can do it like a here:https://github.com/…grations.php#L20

Thanks, is working.