Outdated nette database documentation (Missing 2nd param for Context)

skrivy
Member | 51
+
+3
-

According to the documentation provided here https://doc.nette.org/en/database, this should work:

require __DIR__ . '/../vendor/autoload.php';

use Nette\Database\Connection;
use Nette\Database\Context;

// Connect to the database
$connection = new Connection('mysql:host=127.0.0.1;dbname=...', '...', '...');
$database = new Context($connection);

// Run a query
$database->query('SELECT * FROM ...')->dump();

However I get:

Catchable fatal error: Argument 2 passed to Nette\Database\Context::__construct() must be an instance of Nette\Database\IStructure, none given, called in run.php on line 10 and defined in vendor/nette/database/src/Database/Context.php on line 32

Minimal working code is:

<?php

require __DIR__ . '/../vendor/autoload.php';

use Nette\Database\Connection;
use Nette\Database\Context;

// Connect to the database
$connection = new Connection('mysql:host=127.0.0.1;dbname=...', '...', '...');
$cacheMemoryStorage = new Nette\Caching\Storages\MemoryStorage;
$structure = new Nette\Database\Structure($connection, $cacheMemoryStorage);
$database = new Nette\Database\Context($connection, $structure);

// Run a query
$database->query('SELECT * FROM ...')->dump();

Last edited by skrivy (2016-03-04 10:50)

josef.sabl
Member | 153
+
-1
-

As I don't use Nette\Database for few years I ran into this in a little presentation and it was a bit embarrassing. Why can't the second parameter be optional? Context then should be able create what it needs with default classes? This reminds me of Zend where I had to copy-paste lots of default code. Obsolete documentation then puts quite a roadblocks for newcomers as I can imagine.

Last edited by josef.sabl (2017-04-03 18:30)

greeny
Member | 405
+
0
-

In most cases you should get your Context from DI container. But if you are using Nette/Database standalone, then you of course need to provide services for caching and structure. It is good that you need to provide it, since it removes that magic behaviour similar to well-known case with credit card:

$card = new CreditCard(1234567890);
$card->pay(1000, Currency::CZK); // magically connects to payment gate and creates payment?? how???
Felix
Nette Core | 1183
+
+1
-

@skrivy Thanks for reporting. I've made an issue.

https://github.com/…s/issues/535