Nette Database Core is a database abstraction layer and provides core functionality. It is a wrapper around the PDO and provides core functionality.
Nette Database Explorer significantly simplifies retrieving data from the database without writing SQL queries. It uses efficient queries and does not transmit unnecessary data.
We have been developing Nette for over 10 years- and counting! Libraries we provide are therefore highly mature, stable, and widely used. They are trusted by a number of global corporations and many significant websites rely on us. Who uses and trusts Nette?
Nette Framework provides a powerful layer for accessing your database easily.
The Nette\Database\Connection class is a wrapper around the PDO and represents a connection to the database. The core functionality is provided by Nette\Database\Context. Nette\Database\Table layer provides an enhanced layer for table querying.
To create a new database connection just create a new instance of Nette\Database\Connection class:
use Nette\Database\Connection; $connection = new Connection($dsn, $user, $password);
Nette\Database will create own proper internal driver based on your database server. This internal driver adjusts the Nette\Database behavior for your database server. Nette supports this database servers:
|Database server||DSN name||Database support||Database\Table support|
|MS SQL (PDO_SQLSRV)||sqlsrv||YES||YES|
|MS SQL (PDO_DBLIB)||mssql||YES||–|
The 4th optional parameter of the connection constructor allows you to specify some options. These options are passed to the PDO constructor and the internal driver constructor, so they can contain a configuration for both the PDO instance and the driver instance.
All connections are created as “lazy” by default. This means the connection is established when it's needed, not when you
Connection instance. You can disable this behavior by passing
'lazy' => FALSE
The internal driver class name is detected by provided DSN. However, you can provide you own implementation of an internal
driver. To do this, pass your driver name as an option with the
$connection = new Connection($dsn, $user, $password, array( 'lazy' => FALSE, 'driverClass' => 'App\YourSpecificDriverImplementationName' ));
The easiest way to create a connection is to configure one in your application config. NetteExtension allows you create multiple connections. NetteExtension helps you to add database panel to Tracy debug bar.
nette: database: default: dsn: "mysql:host=127.0.0.1;dbname=test" user: "root" password: "password" options: [PDO::MYSQL_ATTR_COMPRESS = true] debugger: true # debugger bar panel explain: true # explain queries in debugger bar reflection: discovered # or conventional or classname, default is discovered autowired: true anotherConnection: dsn: ... autowired: false
The core functionality is provided by Nette\Database\Context. Database\Context allows you to easily
query your database by calling
use Nette\Database\Context; $database = new Context($connection); $database->query('INSERT INTO users', array( // an array can be a parameter 'name' => 'Jim', 'created' => new DateTime, // or a DateTime object 'avatar' => fopen('image.gif', 'r'), // or a file ), ...); // it is even possible to use multiple inserts $database->query('UPDATE users SET ? WHERE id=?', $data, $id); $database->query('SELECT * FROM categories WHERE id=?', 123)->dump();