Skip to content

Commit abe9849

Browse files
committed
readme.md: updated doc
1 parent 2c000fe commit abe9849

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

readme.md

+28-33
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,54 @@ Nette provides a powerful layer for accessing your database easily.
1313
- easily fetches data
1414
- uses efficient queries and does not transmit unnecessary data
1515

16-
The `Nette\Database\Connection` class is a wrapper around the PDO and represents a connection to the database.
17-
The core functionality is provided by `Nette\Database\Context`. `Nette\Database\Table` layer provides an enhanced layer for table querying.
16+
The [Nette Database Core](https://doc.nette.org/database-core) is a wrapper around the PDO and provides core functionality.
1817

19-
To create a new database connection just create a new instance of [api:Nette\Database\Connection] class:
18+
The [Nette Database Explorer](https://doc.nette.org/database-explorer) layer helps you to fetch database data more easily and in a more optimized way.
2019

21-
```php
22-
$connection = new Nette\Database\Connection($dsn, $user, $password);
23-
```
2420

25-
All connections are created as "lazy" by default. This means the connection is established when it's needed, not when you create a `Connection` instance. You can disable this behavior by passing `'lazy' => FALSE` configuration.
21+
Documentation
22+
-------------
23+
24+
This is just a piece of documentation. [Please see our website](https://doc.nette.org/database).
25+
2626

27-
Queries
28-
--------
27+
Database Core
28+
-------------
2929

30-
The core functionality is provided by `Nette\Database\Connection`. Connection allows you to easily query your database by calling `query` method:
30+
To create a new database connection just create a new instance of `Nette\Database\Connection` class:
3131

3232
```php
33-
$database = new Nette\Database\Context($connection);
33+
$database = new Nette\Database\Connection($dsn, $user, $password); // the same arguments as uses PDO
34+
```
35+
36+
Connection allows you to easily query your database by calling `query` method:
3437

35-
$database->query('INSERT INTO users', array( // an array can be a parameter
38+
```php
39+
$database->query('INSERT INTO users', [ // an array can be a parameter
3640
'name' => 'Jim',
3741
'created' => new DateTime, // or a DateTime object
3842
'avatar' => fopen('image.gif', 'r'), // or a file
39-
), ...); // it is even possible to use multiple inserts
43+
], ...); // it is even possible to use multiple inserts
4044

4145
$database->query('UPDATE users SET ? WHERE id=?', $data, $id);
4246
$database->query('SELECT * FROM categories WHERE id=?', 123)->dump();
4347
```
4448

45-
Table Selection
46-
---------------
49+
Database Explorer
50+
-----------------
51+
52+
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.
4753

48-
`Nette\Database\Table` 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 | database-activerow] instances. Data from other tables connected by relationships are delivered by another queries - this is maintained by Database\Table layer itself.
54+
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.
4955

50-
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\Table does it.
56+
Selecting data starts with the table, just call `$context->table()` on the `Nette\Database\Context` object. The easiest way to get it is [described here](https://doc.nette.org/database-core#toc-configuration), but if we use Nette Database Explorer alone, it can be [manually created](https://doc.nette.org/database-explorer#toc-manual-creating-nette-database-context).
5157

52-
Creating Selection is quite easy, just call `table()` method on your database context.
5358

5459
```php
5560
$selection = $context->table('book'); // db table name is "book"
5661
```
5762

58-
Selection implements traversable interface: you can just iterate over the instance to get all books. The rows are fetched as ActiveRow instances; you can read row data from their properties.
63+
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.
5964

6065
```php
6166
$books = $context->table('book');
@@ -65,7 +70,7 @@ foreach ($books as $book) {
6570
}
6671
```
6772

68-
Getting just one specific row is done by `get()` method. It is "filtering" method, which directly returns an ActiveRow instance.
73+
Getting just one specific row is done by `get()` method, which directly returns an ActiveRow instance.
6974

7075
```php
7176
$book = $context->table('book')->get(2); // returns book with id 2
@@ -76,18 +81,6 @@ echo $book->author_id;
7681
Working with relationships
7782
--------------------------
7883

79-
As we mentioned in the chapter intro, Database\Table layer maintains the table relations for you. There are two possibilities how and where you can work with relationships.
80-
81-
1. **Filtering rows fetched by Selection.** In the introduction we stated the basic principle to select data only from one database table at once. However, Selection instance can do a table join to filter selected row. For example you need select only that authors who has written more than 2 books.
82-
2. **Getting related data for fetched ActiveRows.** We denied getting data from more than one table at once. Sadly, printing `author_id` is not good enough. We need to get full author database row, ideally fetched as ActiveRow. Getting this type of relationships is maintained by ActiveRow.
83-
84-
85-
In provided examples we will work with this database schema below. There are common OneHasMany and ManyHasMany relationships. OneHasMany relationship is doubled, a book must have an author and could have a translator (`translator_id` could be a `NULL`).
86-
87-
![](https://files.nette.org/git/doc-2.1/db-schema-1-.png)
88-
89-
In example below we are getting related data for fetched books. In author property (of book ActiveRow instances) is available another ActiveRow instance, which represents author of the book. Getting book_tag instances is done by `related()` method, which returns collection of this instances. In the cycle we get the tag name from another ActiveRow instance available in book_tag instance.
90-
9184
```php
9285
$books = $context->table('book');
9386

@@ -111,11 +104,13 @@ SELECT * FROM `book_tag` WHERE (`book_tag`.`book_id` IN (1, 4, 2, 3))
111104
SELECT * FROM `tag` WHERE (`tag`.`id` IN (21, 22, 23))
112105
```
113106

114-
If you use cache (defaults on), no columns will be queried unnecessarily. After the first query, cache will store the used column names and Nette\Database will run queries only with the needed columns:
107+
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 will run queries only with the needed columns:
115108

116109
```sql
117110
SELECT `id`, `title`, `author_id` FROM `book`
118111
SELECT `id`, `name` FROM `author` WHERE (`author`.`id` IN (11, 12))
119112
SELECT `book_id`, `tag_id` FROM `book_tag` WHERE (`book_tag`.`book_id` IN (1, 4, 2, 3))
120113
SELECT `id`, `name` FROM `tag` WHERE (`tag`.`id` IN (21, 22, 23))
121114
```
115+
116+
[Continue…](https://doc.nette.org/database-explorer).

0 commit comments

Comments
 (0)