2017 © Pedro Peláez
 

library database

image

nette/database

  • Wednesday, July 11, 2018
  • by david@grudl.com
  • Repository
  • 39 Watchers
  • 129 Stars
  • 1,030,277 Installations
  • PHP
  • 187 Dependents
  • 14 Suggesters
  • 84 Forks
  • 45 Open issues
  • 31 Versions
  • 6 % Grown

The README.md

Nette Database, (*1)

Downloads this Month Tests Latest Stable Version License, (*2)

 , (*3)

Introduction

Nette provides a powerful layer for accessing your database easily., (*4)

✅ composes SQL queries with ease
✅ significantly simplifies retrieving data without writing SQL queries
✅ uses efficient queries and does not transmit unnecessary data, (*5)

The Nette Database Core is a wrapper around the PDO and provides core functionality., (*6)

The Nette Database Explorer layer helps you to fetch database data more easily and in a more optimized way., (*7)

 , (*8)

Support Me

Do you like Nette Database? Are you looking forward to the new features?, (*9)

Buy me a coffee, (*10)

Thank you!, (*11)

 , (*12)

Installation

The recommended way to install is via Composer:, (*13)

composer require nette/database

It requires PHP version 8.1 and supports PHP up to 8.4., (*14)

 , (*15)

Usage

This is just a piece of documentation. Please see our website., (*16)

 , (*17)

Database Core

To create a new database connection just create a new instance of Nette\Database\Connection class:, (*18)

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

Connection allows you to easily query your database by calling query method:, (*19)

$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();

 , (*20)

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., (*21)

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., (*22)

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., (*23)

$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., (*24)

$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., (*25)

$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:, (*26)

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:, (*27)

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…., (*28)

The Versions

11/07 2018

dev-master

9999999-dev https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

database postgresql pdo mssql mysql sqlite nette oracle queries notorm

19/06 2018

v2.4.x-dev

2.4.9999999.9999999-dev https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

database postgresql pdo mssql mysql sqlite nette oracle queries notorm

01/06 2018

v2.4.6

2.4.6.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

database postgresql pdo mssql mysql sqlite nette oracle queries notorm

05/02 2018

v2.4.5

2.4.5.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

database postgresql pdo mssql mysql sqlite nette oracle queries notorm

27/07 2017

v2.3.x-dev

2.3.9999999.9999999-dev https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

27/07 2017

v2.4.4

2.4.4.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

database postgresql pdo mssql mysql sqlite nette oracle queries notorm

12/07 2017

v2.4.3

2.4.3.0 https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

29/06 2017

v2.2.x-dev

2.2.9999999.9999999-dev https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

02/02 2017

v3.0.0-alpha

3.0.0.0-alpha https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

02/02 2017

v2.4.2

2.4.2.0 https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

12/12 2016

v2.3.11

2.3.11.0 https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

18/10 2016

v2.3.10

2.3.10.0 https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

18/10 2016

v2.4.1

2.4.1.0 https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

28/06 2016

v2.4.0

2.4.0.0 https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

30/05 2016

v2.3.9

2.3.9.0 https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

06/02 2016

2.2.x-dev

2.2.9999999.9999999-dev https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

03/12 2015

v2.3.8

2.3.8.0 https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

05/10 2015

v2.3.7

2.3.7.0 https://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

14/09 2015

v2.3.6

2.3.6.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

23/08 2015

v2.3.5

2.3.5.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

20/07 2015

v2.3.4

2.3.4.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

19/07 2015

v2.2.5

2.2.5.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

20/05 2015

v2.3.3

2.3.3.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

22/04 2015

v2.3.2

2.3.2.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

26/03 2015

v2.3.1

2.3.1.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

10/03 2015

v2.2.4

2.2.4.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

20/02 2015

v2.3.0

2.3.0.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

08/11 2014

v2.2.3

2.2.3.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

25/08 2014

v2.2.2

2.2.2.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

25/05 2014

v2.2.1

2.2.1.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

11/05 2014

v2.2.0

2.2.0.0 http://nette.org

Nette Database Component

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires