### Install Nette Database via Composer Source: https://github.com/nette/database/blob/master/readme.md Installs the Nette Database library using Composer. This is the recommended installation method. It requires PHP version 8.1 or higher. ```bash composer require nette/database ``` -------------------------------- ### Example SQL Query Source: https://github.com/nette/database/blob/master/readme.md A sample SQL query that might be executed by the Nette Database layer, demonstrating table selection and potential column names. ```sql SELECT * FROM `book` ``` -------------------------------- ### Get a Specific Row with ActiveRow Source: https://github.com/nette/database/blob/master/readme.md Retrieves a single row from a table by its primary key using the `get()` method. This method directly returns an `ActiveRow` instance for the specified record. ```php $book = $explorer->table('book')->get(2); echo $book->title; echo $book->author_id; ``` -------------------------------- ### Create Nette Database Connection Source: https://github.com/nette/database/blob/master/readme.md Instantiates the Nette Database Explorer, which acts as a wrapper around PDO. It takes DSN, username, and password as arguments, similar to PDO's constructor. ```php $database = new Nette\Database\Explorer($dsn, $user, $password); ``` -------------------------------- ### Initial Database Queries Source: https://github.com/nette/database/blob/master/readme.md These SQL queries represent the initial state before caching is applied, potentially selecting all columns (`*`) or a broader set of columns than strictly needed for subsequent operations. ```sql 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)) ``` -------------------------------- ### Execute Database Queries with Parameters Source: https://github.com/nette/database/blob/master/readme.md Demonstrates executing various SQL queries (INSERT, UPDATE, SELECT) using the `query` method. It supports passing parameters as arrays, DateTime objects, file handles, and using placeholders for safe query execution. ```php $database->query('INSERT INTO users', [ 'name' => 'Jim', 'created' => new DateTime, 'avatar' => fopen('image.gif', 'r'), ], ...); $database->query('UPDATE users SET ? WHERE id=?', $data, $id); $database->query('SELECT * FROM categories WHERE id=?', 123)->dump(); ``` -------------------------------- ### Database Transaction Commands Source: https://github.com/nette/database/blob/master/tests/Database.Tracy/panel.html Demonstrates standard SQL transaction control commands used in database operations. Includes BEGIN TRANSACTION, COMMIT, and ERROR handling. ```sql BEGIN TRANSACTION ``` ```sql COMMIT ``` ```sql ERROR ``` -------------------------------- ### Database Query Execution and Logging Source: https://github.com/nette/database/blob/master/tests/Database.Tracy/panel.html Illustrates the execution of a SQL query within a file context, including potential error reporting. The format suggests logging from a tool that tracks file origins and execution status. ```APIDOC File: Explorer.php Line: [line_number] Status: [SUCCESS|ERROR] Query: [SQL_QUERY_OR_COMMAND] Trace: [Optional_Trace_Information] ``` -------------------------------- ### Fetch Data with Nette Database Explorer Source: https://github.com/nette/database/blob/master/readme.md Shows how to select data from a table using the `table()` method and iterate over the results. Each row is returned as an `ActiveRow` object, allowing property access to column data. ```php $selection = $explorer->table('book'); foreach ($selection as $book) { echo $book->title; echo $book->author_id; } ``` -------------------------------- ### Nette Arrays Utility Function Source: https://github.com/nette/database/blob/master/tests/Database.Tracy/panel.html Shows the usage of the invoke method from the Nette\Utils\Arrays class, likely for applying a callback to array elements. ```php Nette\Utils\Arrays::invoke() ``` -------------------------------- ### Work with Relationships in Nette Database Source: https://github.com/nette/database/blob/master/readme.md Illustrates how to access related data from other tables using properties (e.g., `$book->author->name`) and the `related()` method for many-to-many relationships (e.g., `$book->related('book_tag')`). This approach optimizes query execution. ```php $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 . ', '; } } ``` -------------------------------- ### Basic SQL Query Source: https://github.com/nette/database/blob/master/tests/Database.Tracy/panel.html A simple SQL SELECT statement, often used for health checks or retrieving minimal data. ```sql SELECT 1 ``` -------------------------------- ### Optimized Queries with Caching Source: https://github.com/nette/database/blob/master/readme.md After enabling caching, Nette Database Explorer stores used column names and executes subsequent queries only with the required columns, leading to more efficient data retrieval. ```sql 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)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.