PHPackages                             fabik/database - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. fabik/database

Abandoned → [kdyby/doctrine](/?search=kdyby%2Fdoctrine)Library[Database &amp; ORM](/categories/database)

fabik/database
==============

Database layer for Nette Framework.

1.1.0(13y ago)198902[1 issues](https://github.com/fabik/database/issues)BSD-3-ClausePHP

Since Sep 28Pushed 12y ago4 watchersCompare

[ Source](https://github.com/fabik/database)[ Packagist](https://packagist.org/packages/fabik/database)[ Docs](http://github.com/fabik/database)[ RSS](/packages/fabik-database/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

fabik/database
==============

[](#fabikdatabase)

This is a database layer for Nette Framework based on `Nette\Database`.

Installation
------------

[](#installation)

Get the source code using [Composer](http://getcomposer.org/) (add `"fabik/database": "1.1.*"` to your `composer.json`) or directly download `Database` to your libs directory.

Example of use
--------------

[](#example-of-use)

1. Create the database:

    ```
    CREATE TABLE `users` (
    	`id` int unsigned NOT NULL AUTO_INCREMENT,
    	`username` varchar(255) NOT NULL,
    	`password` char(40) NOT NULL,
    	`email` varchar(255) NOT NULL,
    	`firstname` varchar(255) NOT NULL,
    	`surname` varchar(255) NOT NULL,
    	PRIMARY KEY (`id`),
    	UNIQUE KEY (`username`)
    ) ENGINE=InnoDB;

    CREATE TABLE `articles` (
    	`id` int unsigned NOT NULL AUTO_INCREMENT,
    	`title` varchar(255) NOT NULL,
    	`content` longtext NOT NULL,
    	`author_id` int unsigned NOT NULL,
    	PRIMARY KEY (`id`),
    	KEY (`author_id`),
    	FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
    ) ENGINE=InnoDB;
    ```
2. Register the compiler extension in the bootstrapper.

    ```
    use Fabik\Database\DatabaseExtension;

    $configurator->onCompile[] = function($configurator, $compiler) {
    	$compiler->addExtension('database', new DatabaseExtension);
    };
    ```
3. Add the following sections to your `config.neon` file:

    ```
    nette:
    	database:
    		default:
    			dsn: '%database.driver%:host=%database.host%;dbname=%database.dbname%'
    			user: %database.user%
    			password: %database.password%

    database:
    	rowFactory:
    		classes:
    			articles: Blog\Article
    			users: Blog\User

    services:
    	articles: Blog\Articles
    	users: Blog\Users
    ```
4. Create classes for rows (e.g. `Article`, `User`) and tables (e.g. `Articles`, `Users`):

    ```
