All Projects β†’ nette β†’ Database

nette / Database

Licence: other
πŸ’Ύ A database layer with a familiar PDO-like API but much more powerful. Building queries, advanced joins, drivers for MySQL, PostgreSQL, SQLite, MS SQL Server and Oracle.

Projects that are alternatives of or similar to Database

Http
🌐 Abstraction for HTTP request, response and session. Provides careful data sanitization and utility for URL and cookies manipulation.
Stars: ✭ 223 (-11.16%)
Mutual labels:  nette, nette-framework
Caching
⏱ Caching library with easy-to-use API and many cache backends.
Stars: ✭ 234 (-6.77%)
Mutual labels:  nette, nette-framework
Nette
πŸ‘ͺ METAPACKAGE for Nette Framework components
Stars: ✭ 1,356 (+440.24%)
Mutual labels:  nette, nette-framework
Naja
Modern AJAX library for Nette Framework
Stars: ✭ 86 (-65.74%)
Mutual labels:  nette, nette-framework
Sandbox
Nette Framework sandbox project.
Stars: ✭ 143 (-43.03%)
Mutual labels:  nette, nette-framework
Tracy
😎 Tracy: the addictive tool to ease debugging PHP code for cool developers. Friendly design, logging, profiler, advanced features like debugging AJAX calls or CLI support. You will love it.
Stars: ✭ 1,345 (+435.86%)
Mutual labels:  nette, nette-framework
Safe Stream
SafeStream: atomic and safe manipulation with files via native PHP functions.
Stars: ✭ 101 (-59.76%)
Mutual labels:  nette, nette-framework
Cookbook
🎢 Cookbook for Nette Framework (@nette) & Contributte (@contributte). Read it while its HOT!
Stars: ✭ 30 (-88.05%)
Mutual labels:  nette, nette-framework
Tokenizer
Source code tokenizer
Stars: ✭ 119 (-52.59%)
Mutual labels:  nette, nette-framework
Component Model
βš› Component model foundation for Nette.
Stars: ✭ 117 (-53.39%)
Mutual labels:  nette, nette-framework
Php Generator
🐘 Generates neat PHP code for you. Supports new PHP 8.0 features.
Stars: ✭ 1,264 (+403.59%)
Mutual labels:  nette, nette-framework
Security
πŸ”‘ Provides authentication, authorization and a role-based access control management via ACL (Access Control List)
Stars: ✭ 180 (-28.29%)
Mutual labels:  nette, nette-framework
Utils
πŸ›  Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
Stars: ✭ 1,158 (+361.35%)
Mutual labels:  nette, nette-framework
Datagrid
πŸ’ͺ DataGrid for Nette Framework: filtering, sorting, pagination, tree view, table view, translator, etc
Stars: ✭ 224 (-10.76%)
Mutual labels:  nette, nette-framework
Flexicms
Flexible site management system Flexi CMS
Stars: ✭ 61 (-75.7%)
Mutual labels:  orm, pdo
Docs
πŸ“– The Nette documentation
Stars: ✭ 99 (-60.56%)
Mutual labels:  nette, nette-framework
Flash Messages
Flash messages handler for Nette Framework (2.4+)
Stars: ✭ 8 (-96.81%)
Mutual labels:  nette, nette-framework
Middlewares
πŸ’₯ Middlewares / Relay / PSR-7 support to Nette Framework (@nette)
Stars: ✭ 13 (-94.82%)
Mutual labels:  nette, nette-framework
Ship Hold
data access framework for Postgresql on nodejs
Stars: ✭ 110 (-56.18%)
Mutual labels:  orm, sql-query
Nette.ajax.js
Flexible AJAX for Nette Framework. Supports snippets, redirects etc.
Stars: ✭ 150 (-40.24%)
Mutual labels:  nette, nette-framework

Nette Database

Downloads this Month Tests Build Status Windows Latest Stable Version License

Introduction

Nette provides a powerful layer for accessing your database easily.

  • composes SQL queries with ease
  • easily fetches data
  • uses efficient queries and does not transmit unnecessary data

The Nette Database Core is a wrapper around the PDO and provides core functionality.

The Nette Database Explorer layer helps you to fetch database data more easily and in a more optimized way.

Support Me

Do you like Nette Database? Are you looking forward to the new features?

Buy me a coffee

Thank you!

Installation

The recommended way to install is via Composer:

composer require nette/database

It requires PHP version 8.0.

Usage

This is just a piece of documentation. Please see our website.

Database Core

To create a new database connection just create a new instance of Nette\Database\Connection class:

$database = new Nette\Database\Connection($dsn, $user, $password); // the same arguments as uses PDO

Connection allows you to easily query your database by calling query method:

$database->query('INSERT INTO users', [ // 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();

Database Explorer

Nette Database Explorer layer helps you to fetch database data more easily and in a more optimized way. The primary attitude is to fetch data only from one table and fetch them at once. The data are fetched into ActiveRow instances. Data from other tables connected by relationships are delivered by another queries - this is maintained by Database Explorer layer itself.

Let's take a look at common use-case. You need to fetch books and their authors. It is common 1:N relationship. The often used implementation fetches data by one SQL query with table joins. The second possibility is to fetch data separately, run one query for getting books and then get an author for each book by another query (e.g. in your foreach cycle). This could be easily optimized to run only two queries, one for books, and another for the needed authors - and this is just the way how Nette Database Explorer does it.

Selecting data starts with the table, just call $explorer->table() on the Nette\Database\Explorer object. The easiest way to get it is described here, but if we use Nette Database Explorer alone, it can be manually created.

$selection = $explorer->table('book'); // db table name is "book"

We can simply iterate over the selection and pass through all the books. The rows are fetched as ActiveRow instances; you can read row data from their properties.

$books = $explorer->table('book');
foreach ($books as $book) {
	echo $book->title;
	echo $book->author_id;
}

Getting just one specific row is done by get() method, which directly returns an ActiveRow instance.

$book = $explorer->table('book')->get(2); // returns book with id 2
echo $book->title;
echo $book->author_id;

Working with relationships

$books = $explorer->table('book');

foreach ($books as $book) {
	echo 'title:      ' . $book->title;
	echo 'written by: ' . $book->author->name;

	echo 'tags: ';
	foreach ($book->related('book_tag') as $bookTag) {
		echo $bookTag->tag->name . ', ';
	}
}

You will be pleased how efficiently the database layer works. The example above performs constant number of queries, see following 4 queries:

SELECT * FROM `book`
SELECT * FROM `author` WHERE (`author`.`id` IN (11, 12))
SELECT * FROM `book_tag` WHERE (`book_tag`.`book_id` IN (1, 4, 2, 3))
SELECT * FROM `tag` WHERE (`tag`.`id` IN (21, 22, 23))

If you use caching (defaults on), no columns will be queried unnecessarily. After the first query, cache will store the used column names and Nette Database Explorer will run queries only with the needed columns:

SELECT `id`, `title`, `author_id` FROM `book`
SELECT `id`, `name` FROM `author` WHERE (`author`.`id` IN (11, 12))
SELECT `book_id`, `tag_id` FROM `book_tag` WHERE (`book_tag`.`book_id` IN (1, 4, 2, 3))
SELECT `id`, `name` FROM `tag` WHERE (`tag`.`id` IN (21, 22, 23))

Continue….

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].